# Sort a Linked List in wave form

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 >= list <= list >= …..
```

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 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++

 `// C++ program to sort linked list ` `// in wave form ` `#include ` `#include ` ` `  `using` `namespace` `std; ` ` `  `// A linked list node ` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node* next; ` `}; ` ` `  `// Function to add a node at the ` `// beginning of Linked List ` `void` `push(``struct` `Node** head_ref, ``int` `new_data) ` `{ ` `    ``/* allocate node */` `    ``struct` `Node* new_node = (``struct` `Node*)``malloc``(``sizeof``(``struct` `Node)); ` ` `  `    ``/* put in the data */` `    ``new_node->data = new_data; ` ` `  `    ``/* link the old list off the new node */` `    ``new_node->next = (*head_ref); ` ` `  `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node; ` `} ` ` `  `// Function get size of the list ` `int` `listSize(``struct` `Node* node) ` `{ ` `    ``int` `c = 0; ` ` `  `    ``while` `(node != NULL) { ` `        ``c++; ` ` `  `        ``node = node->next; ` `    ``} ` ` `  `    ``return` `c; ` `} ` ` `  `// Function to print the list ` `void` `printList(``struct` `Node* node) ` `{ ` `    ``while` `(node != NULL) { ` `        ``cout << node->data << ``" "``; ` ` `  `        ``node = node->next; ` `    ``} ` `} ` ` `  `/* UTILITY FUNCTIONS */` `/* Function to swap two integers */` `void` `swap(``int``* a, ``int``* b) ` `{ ` `    ``int` `temp; ` `    ``temp = *a; ` `    ``*a = *b; ` `    ``*b = temp; ` `} ` ` `  `// Function to sort linked list in ` `// wave form ` `void` `sortInWave(``struct` `Node* head) ` `{ ` `    ``struct` `Node* current = head; ` `    ``struct` `Node* prev = NULL; ` ` `  `    ``// Variable to track even position ` `    ``int` `i = 0; ` ` `  `    ``// Size of list ` `    ``int` `n = listSize(head); ` ` `  `    ``// Traverse all even positioned nodes ` `    ``while` `(i < n) { ` ` `  `        ``if` `(i % 2 == 0) { ` `            ``// If current even element is ` `            ``// smaller than previous ` `            ``if` `(i > 0 && (prev->data > current->data)) ` `                ``swap(&(current->data), &(prev->data)); ` ` `  `            ``// If current even element is ` `            ``// smaller than next ` `            ``if` `(i < n - 1 && (current->data < current->next->data)) ` `                ``swap(&(current->data), &(current->next->data)); ` `        ``} ` ` `  `        ``i++; ` ` `  `        ``prev = current; ` `        ``current = current->next; ` `    ``} ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``struct` `Node* start = NULL; ` ` `  `    ``/* The constructed linked list is:  ` `    ``10, 90, 49, 2, 1, 5, 23*/` `    ``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

 `// Java program to sort linked list ` `// in wave form ` `class` `GFG ` `{ ` `     `  `// A linked list node ` `static` `class` `Node  ` `{ ` `    ``int` `data; ` `    ``Node next; ` `}; ` ` `  `// Function to add a node at the ` `// beginning of Linked List ` `static` `Node push(Node head_ref, ``int` `new_data) ` `{ ` `    ``/* allocate node */` `    ``Node new_node = ``new` `Node(); ` ` `  `    ``/* put in the data */` `    ``new_node.data = new_data; ` ` `  `    ``/* link the old list off the new node */` `    ``new_node.next = (head_ref); ` ` `  `    ``/* move the head to point to the new node */` `    ``(head_ref) = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `// Function get size of the list ` `static` `int` `listSize( Node node) ` `{ ` `    ``int` `c = ``0``; ` `    ``while` `(node != ``null``)  ` `    ``{ ` `        ``c++; ` `        ``node = node.next; ` `    ``} ` `    ``return` `c; ` `} ` ` `  `// Function to print the list ` `static` `void` `printList( Node node) ` `{ ` `    ``while` `(node != ``null``)  ` `    ``{ ` `        ``System.out.print(node.data + ``" "``); ` `        ``node = node.next; ` `    ``} ` `} ` ` `  `// Function to sort linked list in ` `// wave form ` `static` `Node sortInWave( Node head) ` `{ ` `    ``Node current = head; ` `    ``Node prev = ``null``; ` ` `  `    ``// Variable to track even position ` `    ``int` `i = ``0``; ` ` `  `    ``// Size of list ` `    ``int` `n = listSize(head); ` ` `  `    ``// Traverse all even positioned nodes ` `    ``while` `(i < n)  ` `    ``{ ` `        ``if` `(i % ``2` `== ``0``) ` `        ``{ ` `            ``// If current even element is ` `            ``// smaller than previous ` `            ``if` `(i > ``0` `&& (prev.data > current.data)) ` `            ``{ ` `                ``int` `t = prev.data; ` `                ``prev.data = current.data; ` `                ``current.data = t; ` `            ``} ` `                 `  `            ``// If current even element is ` `            ``// smaller than next ` `            ``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; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``Node start = ``null``; ` ` `  `    ``/* The constructed linked list is:  ` `    ``10, 90, 49, 2, 1, 5, 23*/` `    ``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); ` `} ` `} ` ` `  `// This code is contributed by Arnab Kundu `

Output:

```90 10 49 1 5 2 23
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Akanksha_Rai, andrew1234

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.