# Sort a linked list that is sorted alternating ascending and descending orders?

Given a Linked List. The Linked List is in alternating ascending and descending orders. Sort the list efficiently.

Example:

```Input List:   10->40->53->30->67->12->89->NULL
Output List:  10->12->30->43->53->67->89->NULL```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A Simple Solution is to use Merge Sort for linked List. This solution takes O(n Log n) time.

An Efficient Solution works in O(n) time. Below are all steps.
1. Separate two lists.
2. Reverse the one with descending order
3. Merge both lists.

Below are C++ and Java implementations of above algorithm.

## C++

```// C++ program to sort a linked list that is alternatively
// sorted in increasing and decreasing order
#include<bits/stdc++.h>
using namespace std;

struct Node
{
int data;
struct Node *next;
};

// This is the main function that sorts the
{
// Split the list into lists

// Reverse the descending linked list

// Merge the two linked lists
}

// A utility function to create a new node
Node* newNode(int key)
{
Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}

// A utility function to reverse a linked list
{
Node* prev = NULL, *curr = head, *next;
while (curr)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
}

// A utility function to print a linked list
{
{
cout << head->data << " ";
}
cout << endl;
}

// A utility function to merge two sorted linked lists
{
// Base cases

Node *temp = NULL;
{
}
else
{
}
return temp;
}

// For example, 10->20->30->15->40->7 is splitted into 10->30->40
// and 20->15->7
{
// Create two dummy nodes to initialize heads of two linked list

while (curr)
{
ascn->next = curr;
ascn = ascn->next;
curr = curr->next;

if (curr)
{
dscn->next = curr;
dscn = dscn->next;
curr = curr->next;
}
}

ascn->next = NULL;
dscn->next = NULL;
}

// Driver program to test above function
int main()
{

cout << "Given Linked List is " << endl;

cout << "Sorted Linked List is " << endl;

return 0;
}```

## Java

```// Java program to sort a linked list that is alternatively
// sorted in increasing and decreasing order
{

class Node
{
int data;
Node next;
Node(int d)   { data = d;  next = null; }
}

Node newNode(int key)
{
return new Node(key);
}

/* This is the main function that sorts
void sort()
{
/* Create 2 dummy nodes and initialise as

// Split the list into lists

// reverse the descending list

// merge the 2 linked lists
}

/* Function to reverse the linked list */
{
Node prev = null;
Node next;
while (current != null)
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
}

/* Function to print linked list */
void printList()
{
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
System.out.println();
}

// A utility function to merge two sorted linked lists
{
// Base cases

Node temp = null;
{
}
else
{
}
return temp;
}

// For example, 10->20->30->15->40->7 is splitted into 10->30->40
// and 20->15->7
{

while (curr != null)
{
// Link alternate nodes in ascending order
ascn.next = curr;
ascn = ascn.next;
curr = curr.next;

if (curr != null)
{
dscn.next = curr;
dscn = dscn.next;
curr = curr.next;
}
}

ascn.next = null;
dscn.next = null;
}

/* Drier program to test above functions */
public static void main(String args[])
{

llist.printList();

llist.sort();

llist.printList();
}

} /* This code is contributed by Rajat Mishra */
```

## Python

```
# Python program to sort a linked list that is alternatively
# sorted in increasing and decreasing order
def __init__(self):

class Node(object):
def __init__(self, d):
self.data = d
self.next = None

def newNode(self, key):
return self.Node(key)

# This is the main function that sorts
def sort(self):
# Create 2 dummy nodes and initialise as
# Split the list into lists
# reverse the descending list
# merge the 2 linked lists

# Function to reverse the linked list
prev = None
while current != None:
self._next = current.next
current.next = prev
prev = current
current = self._next

# Function to print linked list
def printList(self):
while temp != None:
print temp.data,
temp = temp.next
print ''

# A utility function to merge two sorted linked lists
# Base cases
temp = None
else:
return temp

# For example, 10->20->30->15->40->7 is splitted into 10->30->40
# and 20->15->7
while curr != None:
# Link alternate nodes in ascending order
ascn.next = curr
ascn = ascn.next
curr = curr.next
if curr != None:
dscn.next = curr
dscn = dscn.next
curr = curr.next
ascn.next = None
dscn.next = None

# Driver program

llist.printList()

llist.sort()

llist.printList()

# This code is contributed by BHAVYA JAIN

```

Output:
```Given Linked List is
10 40 53 30 67 12 89
10 12 30 40 53 67 89```

Thanks to Gaurav Ahirwar for suggesting this method here.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.7 Average Difficulty : 2.7/5.0
Based on 76 vote(s)