Given an unsorted Linked List of integers. The task is to sort the Linked List into a wave like Line. A Linked List is said to be sorted in Wave Form if the list after sorting is in the form:
list[0] >= list[1] <= list[2] >= …..
Where list[i] denotes the data at i-th node of the Linked List.
Examples:
Input : List = 2 -> 4 -> 6 -> 8 -> 10 -> 20
Output : 4 -> 2 -> 8 -> 6 -> 20 -> 10
Input : List = 3 -> 6 -> 5 -> 10 -> 7 -> 20
Output : 6 -> 3 -> 10 -> 5 -> 20 -> 7
A Simple Solution is to use sorting. First sort the input linked list, then swap all adjacent elements.
For example, let the input list be 3 -> 6 -> 5 -> 10 -> 7 -> 20. After sorting, we get 3 -> 5 -> 6 -> 7 -> 10 -> 20. After swapping adjacent elements, we get 5 -> 3 -> 7 -> 6 -> 20 -> 10 which is the required list in wave form.
Time Complexity: O(N*logN), where N is the number nodes in the list.
Efficient Solution: This can be done in O(n) time by doing a single traversal of the given list. The idea is based on the fact that if we make sure that all even positioned (at index 0, 2, 4, ..) elements are greater than their adjacent odd elements, we don’t need to worry about oddly positioned element. Following are simple steps.
Note: Assuming indexes in list starts from zero. That is, list[0] represents the first elements of the linked list.
Traverse all even positioned elements of input linked list, and do following.
- If current element is smaller than previous odd element, swap previous and current.
- If current element is smaller than next odd element, swap next and current.
Below is the implementation of above approach:
C++
#include <climits>
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int listSize( struct Node* node)
{
int c = 0;
while (node != NULL) {
c++;
node = node->next;
}
return c;
}
void printList( struct Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
void swap( int * a, int * b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void sortInWave( struct Node* head)
{
struct Node* current = head;
struct Node* prev = NULL;
int i = 0;
int n = listSize(head);
while (i < n) {
if (i % 2 == 0) {
if (i > 0 && (prev->data > current->data))
swap(&(current->data), &(prev->data));
if (i < n - 1 && (current->data < current->next->data))
swap(&(current->data), &(current->next->data));
}
i++;
prev = current;
current = current->next;
}
}
int main()
{
struct Node* start = NULL;
push(&start, 23);
push(&start, 5);
push(&start, 1);
push(&start, 2);
push(&start, 49);
push(&start, 90);
push(&start, 10);
sortInWave(start);
printList(start);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int listSize( Node node)
{
int c = 0 ;
while (node != null )
{
c++;
node = node.next;
}
return c;
}
static void printList( Node node)
{
while (node != null )
{
System.out.print(node.data + " " );
node = node.next;
}
}
static Node sortInWave( Node head)
{
Node current = head;
Node prev = null ;
int i = 0 ;
int n = listSize(head);
while (i < n)
{
if (i % 2 == 0 )
{
if (i > 0 && (prev.data > current.data))
{
int t = prev.data;
prev.data = current.data;
current.data = t;
}
if (i < n - 1 && (current.data <
current.next.data))
{
int t = current.next.data;
current.next.data = current.data;
current.data = t;
}
}
i++;
prev = current;
current = current.next;
}
return head;
}
public static void main(String args[])
{
Node start = null ;
start = push(start, 23 );
start = push(start, 5 );
start = push(start, 1 );
start = push(start, 2 );
start = push(start, 49 );
start = push(start, 90 );
start = push(start, 10 );
start = sortInWave(start);
printList(start);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def push( head_ref, new_data):
new_node = Node()
new_node.data = new_data;
new_node. next = (head_ref);
(head_ref) = new_node;
return head_ref
def listSize(node):
c = 0 ;
while (node ! = None ):
c + = 1
node = node. next ;
return c;
def printList(node):
while (node ! = None ):
print (node.data, end = ' ' )
node = node. next ;
def sortInWave(head):
current = head;
prev = None ;
i = 0 ;
n = listSize(head);
while (i < n):
if (i % 2 = = 0 ):
if (i > 0 and (prev.data > current.data)):
(current.data), (prev.data) = (prev.data), (current.data)
if (i < n - 1 and (current.data < current. next .data)):
(current.data), (current. next .data) = (current. next .data), (current.data)
i + = 1
prev = current;
current = current. next ;
if __name__ = = '__main__' :
start = None ;
start = push(start, 23 );
start = push(start, 5 );
start = push(start, 1 );
start = push(start, 2 );
start = push(start, 49 );
start = push(start, 90 );
start = push(start, 10 )
sortInWave(start)
printList(start);
|
C#
using System;
class GFG{
class Node
{
public int data;
public Node next;
};
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static int listSize( Node node)
{
int c = 0;
while (node != null )
{
c++;
node = node.next;
}
return c;
}
static void printList( Node node)
{
while (node != null )
{
Console.Write(node.data + " " );
node = node.next;
}
}
static Node sortInWave( Node head)
{
Node current = head;
Node prev = null ;
int i = 0;
int n = listSize(head);
while (i < n)
{
if (i % 2 == 0)
{
if (i > 0 && (prev.data >
current.data))
{
int t = prev.data;
prev.data = current.data;
current.data = t;
}
if (i < n - 1 && (current.data <
current.next.data))
{
int t = current.next.data;
current.next.data = current.data;
current.data = t;
}
}
i++;
prev = current;
current = current.next;
}
return head;
}
public static void Main( string []args)
{
Node start = null ;
start = push(start, 23);
start = push(start, 5);
start = push(start, 1);
start = push(start, 2);
start = push(start, 49);
start = push(start, 90);
start = push(start, 10);
start = sortInWave(start);
printList(start);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function listSize(node) {
var c = 0;
while (node != null ) {
c++;
node = node.next;
}
return c;
}
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
}
function sortInWave(head) {
var current = head;
var prev = null ;
var i = 0;
var n = listSize(head);
while (i < n) {
if (i % 2 == 0) {
if (i > 0 && prev.data > current.data) {
var t = prev.data;
prev.data = current.data;
current.data = t;
}
if (i < n - 1 && current.data < current.next.data) {
var t = current.next.data;
current.next.data = current.data;
current.data = t;
}
}
i++;
prev = current;
current = current.next;
}
return head;
}
var start = null ;
start = push(start, 23);
start = push(start, 5);
start = push(start, 1);
start = push(start, 2);
start = push(start, 49);
start = push(start, 90);
start = push(start, 10);
start = sortInWave(start);
printList(start);
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Nov, 2022
Like Article
Save Article