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 biotonic 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 the last element of a doubly linked list. Compare both the elements and add the smaller element to result in a list. The advance pointer of that element to the next adjacent element. Repeat this until all elements of input doubly linked list are added to result in the list.
Below is the implementation of above algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* sort( struct Node* head)
{
if (head == NULL || head->next == NULL) {
return head;
}
Node* front = head;
Node* last = head;
Node* res = new Node;
Node* resEnd = res;
Node* next;
while (last->next != NULL) {
last = last->next;
}
while (front != last) {
if (last->data <= front->data) {
resEnd->next = last;
next = last->prev;
last->prev->next = NULL;
last->prev = resEnd;
last = next;
resEnd = resEnd->next;
}
else {
resEnd->next = front;
next = front->next;
front->next = NULL;
front->prev = resEnd;
front = next;
resEnd = resEnd->next;
}
}
resEnd->next = front;
front->prev = resEnd;
return res->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->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void printList( struct Node* head)
{
if (head == NULL)
cout << "Doubly Linked list empty" ;
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
struct Node* head = NULL;
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);
head = sort(head);
cout << "\nDoubly linked list after sorting:\n" ;
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
Node prev;
};
static Node sort(Node head)
{
if (head == null || head.next == null )
{
return head;
}
Node front = head;
Node last = head;
Node res = new Node();
Node resEnd = res;
Node next;
while (last.next != null )
{
last = last.next;
}
while (front != last)
{
if (last.data <= front.data)
{
resEnd.next = last;
next = last.prev;
last.prev.next = null ;
last.prev = resEnd;
last = next;
resEnd = resEnd.next;
}
else
{
resEnd.next = front;
next = front.next;
front.next = null ;
front.prev = resEnd;
front = next;
resEnd = resEnd.next;
}
}
resEnd.next = front;
front.prev = resEnd;
return res.next;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static void printList(Node head)
{
if (head == null )
System.out.print( "Doubly Linked list empty" );
while (head != null )
{
System.out.print( head.data + " " );
head = head.next;
}
}
public static void main(String args[])
{
Node head = null ;
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);
head = sort(head);
System.out.print( "\nDoubly linked list after sorting:\n" );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .prev = None
self . next = None
def sort(head):
if head = = None or head. next = = None :
return head
front = head
last = head
res = Node( None )
resEnd = res
while last. next ! = None :
last = last. next
while front ! = last:
if last.data < = front.data:
resEnd. next = last
next = last.prev
last.prev. next = None
last.prev = resEnd
last = next
resEnd = resEnd. next
else :
resEnd. next = front
next = front. next
front. next = None
front.prev = resEnd
front = next
resEnd = resEnd. next
resEnd. next = front
front.prev = resEnd
return res. next
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.prev = None
new_node. next = head_ref
if head_ref ! = None :
head_ref.prev = new_node
head_ref = new_node
return head_ref
def printList(head):
if head = = None :
print ( "Doubly Linked list empty" )
while head ! = None :
print (head.data, end = " " )
head = head. next
if __name__ = = '__main__' :
head = None
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)
head = sort(head)
print ( "\nDoubly linked list after sorting:" )
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
public Node prev;
};
static Node sort(Node head)
{
if (head == null || head.next == null )
{
return head;
}
Node front = head;
Node last = head;
Node res = new Node();
Node resEnd = res;
Node next;
while (last.next != null )
{
last = last.next;
}
while (front != last)
{
if (last.data <= front.data)
{
resEnd.next = last;
next = last.prev;
last.prev.next = null ;
last.prev = resEnd;
last = next;
resEnd = resEnd.next;
}
else
{
resEnd.next = front;
next = front.next;
front.next = null ;
front.prev = resEnd;
front = next;
resEnd = resEnd.next;
}
}
resEnd.next = front;
front.prev = resEnd;
return res.next;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = (head_ref);
if ((head_ref) != null )
(head_ref).prev = new_node;
(head_ref) = new_node;
return head_ref;
}
static void printList(Node head)
{
if (head == null )
Console.Write( "Doubly Linked list empty" );
while (head != null )
{
Console.Write( head.data + " " );
head = head.next;
}
}
public static void Main(String []args)
{
Node head = null ;
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);
head = sort(head);
Console.Write( "\nDoubly linked list after sorting:\n" );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
this .prev = null ;
}
}
function sort(head) {
if (head == null || head.next == null ) {
return head;
}
var front = head;
var last = head;
var res = new Node();
var resEnd = res;
var next;
while (last.next != null ) {
last = last.next;
}
while (front != last) {
if (last.data <= front.data) {
resEnd.next = last;
next = last.prev;
last.prev.next = null ;
last.prev = resEnd;
last = next;
resEnd = resEnd.next;
}
else {
resEnd.next = front;
next = front.next;
front.next = null ;
front.prev = resEnd;
front = next;
resEnd = resEnd.next;
}
}
resEnd.next = front;
front.prev = resEnd;
return res.next;
}
function push(head_ref, new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.prev = null ;
new_node.next = head_ref;
if (head_ref != null ) head_ref.prev = new_node;
head_ref = new_node;
return head_ref;
}
function printList(head) {
if (head == null ) document.write( "Doubly Linked list empty" );
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = null ;
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);
document.write( "Original Doubly linked list: <br>" );
printList(head);
head = sort(head);
document.write( "<br>Doubly linked list after sorting:<br>" );
printList(head);
</script>
|
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
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...