Sort the biotonic doubly linked list | Set-2

Sort the given biotonic doubly linked list. A biotonic doubly linked list is a doubly linked list which is first increasing and then decreasing. A strictly increasing or a strictly decreasing list is also a biotonic doubly linked list.

Examples:

Input : 2 5 7 12 10 6 4 1
Output : 1 2 4 5 6 7 10 12

Input : 20 17 14 8 3
Output : 3 8 14 17 20

In the previous post, we split the bitonic doubly linked list, reverse the second half and then merge both halves. In this post another alternative method is discussed. The idea is to maintain two pointers, one pointing to head element initially and other pointing to last element of doubly linked list. Compare both the elements and add the smaller element to result list. Advance pointer of that element to next adjacent element. Repeat this until all elements of input doubly linked list are added to result list.

Below is the implementation of above algorithm:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to sort the biotonic
// doubly linked list
  
#include <bits/stdc++.h>
using namespace std;
  
// structure of node of the doubly linked list
struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};
  
// function to sort a biotonic doubly linked list
struct Node* sort(struct Node* head)
{
    // If number of elements are less than or
    // equal to 1 then return.
    if (head == NULL || head->next == NULL) {
        return head;
    }
  
    // Pointer to first element of doubly
    // linked list.
    Node* front = head;
  
    // Pointer to last element of doubly
    // linked list.
    Node* last = head;
  
    // Dummy node to which resultant
    // sorted list is added.
    Node* res = new Node;
  
    // Node after which next element
    // of sorted list is added.
    Node* resEnd = res;
  
    // Node to store next element to
    // which pointer is moved after
    // element pointed by that pointer
    // is added to result list.
    Node* next;
  
    // Find last element of input list.
    while (last->next != NULL) {
        last = last->next;
    }
  
    // Compare first and last element
    // until both pointers are not equal.
    while (front != last) {
  
        // If last element data is less than
        // or equal to front element data,
        // then add last element to
        // result list and change the
        // last pointer to its previous
        // element.
        if (last->data <= front->data) {
            resEnd->next = last;
            next = last->prev;
            last->prev->next = NULL;
            last->prev = resEnd;
            last = next;
            resEnd = resEnd->next;
        }
  
        // If front element is smaller, then
        // add it to result list and change
        // front pointer to its next element.
        else {
            resEnd->next = front;
            next = front->next;
            front->next = NULL;
            front->prev = resEnd;
            front = next;
            resEnd = resEnd->next;
        }
    }
  
    // Add the single element left to the
    // result list.
    resEnd->next = front;
    front->prev = resEnd;
  
    // The head of required sorted list is
    // next to dummy node res.
    return res->next;
}
  
// Function to insert a node at the beginning
// of the Doubly 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;
  
    // since we are adding at the begining,
    // prev is always NULL
    new_node->prev = NULL;
  
    // link the old list off the new node
    new_node->next = (*head_ref);
  
    // change prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
  
    // move the head to point to the new node
    (*head_ref) = new_node;
}
  
// Function to print nodes in a given doubly
// linked list
void printList(struct Node* head)
{
    // if list is empty
    if (head == NULL)
        cout << "Doubly Linked list empty";
  
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}
  
// Driver program to test above
int main()
{
    struct Node* head = NULL;
  
    // Create the doubly linked list:
    // 2<->5<->7<->12<->10<->6<->4<->1
    push(&head, 1);
    push(&head, 4);
    push(&head, 6);
    push(&head, 10);
    push(&head, 12);
    push(&head, 7);
    push(&head, 5);
    push(&head, 2);
  
    cout << "Original Doubly linked list:\n";
    printList(head);
  
    // sort the biotonic DLL
    head = sort(head);
  
    cout << "\nDoubly linked list after sorting:\n";
    printList(head);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to sort the biotonic 
// doubly linked list 
class GFG
{
      
// structure of node of the doubly linked list 
static class Node
    int data; 
    Node next; 
    Node prev; 
}; 
  
// function to sort a biotonic doubly linked list 
static Node sort(Node head) 
    // If number of elements are less than or 
    // equal to 1 then return. 
    if (head == null || head.next == null
    
        return head; 
    
  
    // Pointer to first element of doubly 
    // linked list. 
    Node front = head; 
  
    // Pointer to last element of doubly 
    // linked list. 
    Node last = head; 
  
    // Dummy node to which resultant 
    // sorted list is added. 
    Node res = new Node(); 
  
    // Node after which next element 
    // of sorted list is added. 
    Node resEnd = res; 
  
    // Node to store next element to 
    // which pointer is moved after 
    // element pointed by that pointer 
    // is added to result list. 
    Node next; 
  
    // Find last element of input list. 
    while (last.next != null)
    
        last = last.next; 
    
  
    // Compare first and last element 
    // until both pointers are not equal. 
    while (front != last)
    
  
        // If last element data is less than 
        // or equal to front element data, 
        // then add last element to 
        // result list and change the 
        // last pointer to its previous 
        // element. 
        if (last.data <= front.data)
        
            resEnd.next = last; 
            next = last.prev; 
            last.prev.next = null
            last.prev = resEnd; 
            last = next; 
            resEnd = resEnd.next; 
        
  
        // If front element is smaller, then 
        // add it to result list and change 
        // front pointer to its next element. 
        else
        
            resEnd.next = front; 
            next = front.next; 
            front.next = null
            front.prev = resEnd; 
            front = next; 
            resEnd = resEnd.next; 
        
    
  
    // Add the single element left to the 
    // result list. 
    resEnd.next = front; 
    front.prev = resEnd; 
  
    // The head of required sorted list is 
    // next to dummy node res. 
    return res.next; 
  
// Function to insert a node at the beginning 
// of the Doubly 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; 
  
    // since we are adding at the begining, 
    // prev is always null 
    new_node.prev = null
  
    // link the old list off the new node 
    new_node.next = (head_ref); 
  
    // change prev of head node to new node 
    if ((head_ref) != null
        (head_ref).prev = new_node; 
  
    // move the head to point to the new node 
    (head_ref) = new_node; 
    return head_ref;
  
// Function to print nodes in a given doubly 
// linked list 
static void printList(Node head) 
    // if list is empty 
    if (head == null
        System.out.print( "Doubly Linked list empty"); 
  
    while (head != null)
    
        System.out.print( head.data + " "); 
        head = head.next; 
    
  
// Driver code 
public static void main(String args[])
    Node head = null
  
    // Create the doubly linked list: 
    // 2<.5<.7<.12<.10<.6<.4<.1 
    head = push(head, 1); 
    head = push(head, 4); 
    head = push(head, 6); 
    head = push(head, 10); 
    head = push(head, 12); 
    head = push(head, 7); 
    head = push(head, 5); 
    head = push(head, 2); 
  
    System.out.print("Original Doubly linked list:\n"); 
    printList(head); 
  
    // sort the biotonic DLL 
    head = sort(head); 
  
    System.out.print("\nDoubly linked list after sorting:\n"); 
    printList(head); 
  
// This code is contributed by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to sort 
# the biotonic doubly linked list 
  
# structure of node of the doubly linked list 
class Node:  
      
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None
   
# function to sort a biotonic doubly linked list 
def sort(head): 
   
    # If number of elements are less 
    # than or equal to 1 then return. 
    if head == None or head.next == None:  
        return head 
  
    # Pointer to first element 
    # of doubly linked list. 
    front = head 
  
    # Pointer to last element 
    # of doubly linked list. 
    last = head 
  
    # Dummy node to which resultant 
    # sorted list is added. 
    res = Node(None
  
    # Node after which next element 
    # of sorted list is added. 
    resEnd = res 
  
    # Find last element of input list. 
    while last.next != None
        last = last.next 
       
    # Compare first and last element 
    # until both pointers are not equal. 
    while front != last:  
  
        # If last element data is less than 
        # or equal to front element data, 
        # then add last element to 
        # result list and change the last 
        # pointer to its previous element. 
        if last.data <= front.data:
            resEnd.next = last 
            next = last.prev 
            last.prev.next = None 
            last.prev = resEnd 
            last = next 
            resEnd = resEnd.next 
           
        # If front element is smaller, then 
        # add it to result list and change 
        # front pointer to its next element. 
        else:  
            resEnd.next = front 
            next = front.next 
            front.next = None 
            front.prev = resEnd 
            front = next 
            resEnd = resEnd.next 
  
    # Add the single element left to the 
    # result list. 
    resEnd.next = front 
    front.prev = resEnd 
  
    # The head of required sorted list is 
    # next to dummy node res. 
    return res.next 
   
# Function to insert a node at the 
# beginning of the Doubly Linked List 
def push(head_ref, new_data): 
   
    # put in the data 
    new_node = Node(new_data) 
  
    # since we are adding at the 
    # begining, prev is always None 
    new_node.prev = None 
  
    # link the old list off the new node 
    new_node.next = head_ref
  
    # change prev of head node to new node 
    if head_ref != None
        head_ref.prev = new_node 
  
    # move the head to point to the new node 
    head_ref = new_node
    return head_ref
  
# Function to print nodes in 
# a given doubly linked list 
def printList(head): 
   
    # If list is empty 
    if head == None
        print("Doubly Linked list empty"
  
    while head != None:  
        print(head.data, end = " "
        head = head.next 
  
# Driver program to test above 
if __name__ == '__main__':
   
    head = None 
  
    # Create the doubly linked list: 
    # 2<.5<.7<.12<.10<.6<.4<.1 
    head = push(head, 1
    head = push(head, 4
    head = push(head, 6
    head = push(head, 10
    head = push(head, 12
    head = push(head, 7
    head = push(head, 5
    head = push(head, 2
  
    print("Original Doubly linked list:"
    printList(head) 
  
    # sort the biotonic DLL 
    head = sort(head)
  
    print("\nDoubly linked list after sorting:"
    printList(head)
          
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to sort the biotonic 
// doubly linked list 
using System;
  
class GFG
{
      
// structure of node of the doubly linked list 
public class Node
    public int data; 
    public Node next; 
    public Node prev; 
}; 
  
// function to sort a biotonic doubly linked list 
static Node sort(Node head) 
    // If number of elements are less than or 
    // equal to 1 then return. 
    if (head == null || head.next == null
    
        return head; 
    
  
    // Pointer to first element of doubly 
    // linked list. 
    Node front = head; 
  
    // Pointer to last element of doubly 
    // linked list. 
    Node last = head; 
  
    // Dummy node to which resultant 
    // sorted list is added. 
    Node res = new Node(); 
  
    // Node after which next element 
    // of sorted list is added. 
    Node resEnd = res; 
  
    // Node to store next element to 
    // which pointer is moved after 
    // element pointed by that pointer 
    // is added to result list. 
    Node next; 
  
    // Find last element of input list. 
    while (last.next != null)
    
        last = last.next; 
    
  
    // Compare first and last element 
    // until both pointers are not equal. 
    while (front != last)
    
  
        // If last element data is less than 
        // or equal to front element data, 
        // then add last element to 
        // result list and change the 
        // last pointer to its previous 
        // element. 
        if (last.data <= front.data)
        
            resEnd.next = last; 
            next = last.prev; 
            last.prev.next = null
            last.prev = resEnd; 
            last = next; 
            resEnd = resEnd.next; 
        
  
        // If front element is smaller, then 
        // add it to result list and change 
        // front pointer to its next element. 
        else
        
            resEnd.next = front; 
            next = front.next; 
            front.next = null
            front.prev = resEnd; 
            front = next; 
            resEnd = resEnd.next; 
        
    
  
    // Add the single element left to the 
    // result list. 
    resEnd.next = front; 
    front.prev = resEnd; 
  
    // The head of required sorted list is 
    // next to dummy node res. 
    return res.next; 
  
// Function to insert a node at the beginning 
// of the Doubly 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; 
  
    // since we are adding at the begining, 
    // prev is always null 
    new_node.prev = null
  
    // link the old list off the new node 
    new_node.next = (head_ref); 
  
    // change prev of head node to new node 
    if ((head_ref) != null
        (head_ref).prev = new_node; 
  
    // move the head to point to the new node 
    (head_ref) = new_node; 
    return head_ref;
  
// Function to print nodes in a given doubly 
// linked list 
static void printList(Node head) 
    // if list is empty 
    if (head == null
        Console.Write( "Doubly Linked list empty"); 
  
    while (head != null)
    
        Console.Write( head.data + " "); 
        head = head.next; 
    
  
// Driver code 
public static void Main(String []args)
    Node head = null
  
    // Create the doubly linked list: 
    // 2<.5<.7<.12<.10<.6<.4<.1 
    head = push(head, 1); 
    head = push(head, 4); 
    head = push(head, 6); 
    head = push(head, 10); 
    head = push(head, 12); 
    head = push(head, 7); 
    head = push(head, 5); 
    head = push(head, 2); 
  
    Console.Write("Original Doubly linked list:\n"); 
    printList(head); 
  
    // sort the biotonic DLL 
    head = sort(head); 
  
    Console.Write("\nDoubly linked list after sorting:\n"); 
    printList(head); 
}
  
// This code contributed by Rajput-Ji

chevron_right



Output:

Original Doubly linked list:
2 5 7 12 10 6 4 1
Doubly linked list after sorting:
1 2 4 5 6 7 10 12 

Time Complexity: O(n)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

A Programmer and A Machine learning Enthusiast

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.