Sort given Binary Doubly Linked List without modifying the data
Last Updated :
30 Jan, 2023
Given a head and a tail of a doubly linked list containing 0s and 1s, the task is to sort the Doubly linked list without modifying the data.
Examples:
Input: head = 1->1->0->0->1->0->1->1->0->0->NULL
Output: 0->0->0->0->0->1->1->1->1->1->NULL
Input: head = 1->0->NULL
Output: 0->1->NULL
Approach: The idea to solve this problem is to use two pointers algorithm as mentioned below:
- Create two pointers – head and tail such that head points at first element and tail points at last element
- Keep checking the element at the pointers for 1 at head and 0 at tail,
- Swap the elements whenever 1, 0 combination is found.
Follow the steps to solve the problem:
- Initialize first pointer = head, last pointer = tail
- While first != last and first -> prev != last
- Check, if first->data == 0
- update, first = first->next
- If, last->data == 1
- update, last = last->prev
- If, first->data == 1 and last->data == 0
- then, swap first and last node
- again swap first and last pointer
- move first towards right and last towards left by 1
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class Node {
public :
int data;
Node* prev;
Node* next;
Node( int data)
{
this ->data = data;
this ->prev = NULL;
this ->next = NULL;
}
};
void print(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
void push(Node*& head, Node*& tail, int item)
{
if (tail == NULL) {
Node* temp = new Node(item);
tail = temp;
head = temp;
}
else {
Node* temp = new Node(item);
tail->next = temp;
temp->prev = tail;
tail = temp;
}
}
void swap(Node*& Node1, Node*& Node2)
{
Node* temp;
temp = Node1->next;
Node1->next = Node2->next;
Node2->next = temp;
if (Node1->next != NULL)
Node1->next->prev = Node1;
if (Node2->next != NULL)
Node2->next->prev = Node2;
temp = Node1->prev;
Node1->prev = Node2->prev;
Node2->prev = temp;
if (Node1->prev != NULL)
Node1->prev->next = Node1;
if (Node2->prev != NULL)
Node2->prev->next = Node2;
}
void sort(Node*& head, Node*& tail)
{
if (head == tail || head == NULL)
return ;
Node* first = head;
Node* last = tail;
while ((first != last)
&& (first->prev != last)) {
if (first->data == 0)
first = first->next;
if (last->data == 1)
last = last->prev;
if (first->data == 1 && last->data == 0
&& first->prev != last) {
swap(first, last);
if (head == first)
head = last;
if (tail == last)
tail = first;
Node* Temp = first;
first = last->next;
last = Temp->prev;
}
}
}
int main()
{
Node* head = NULL;
Node* tail = NULL;
push(head, tail, 1);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 0);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 1);
push(head, tail, 1);
push(head, tail, 0);
push(head, tail, 0);
sort(head, tail);
print(head);
return 0;
}
|
Java
import java.util.*;
class GFG{
static Node head;
static Node tail;
static class Node {
int data;
Node prev;
Node next;
Node( int data)
{
this .data = data;
this .prev = null ;
this .next = null ;
}
};
static void print(Node head)
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data+ " " );
temp = temp.next;
}
System.out.println();
}
static void push( int item)
{
if (tail == null ) {
Node temp = new Node(item);
tail = temp;
head = temp;
}
else {
Node temp = new Node(item);
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
static void swap(Node Node1, Node Node2)
{
Node temp;
temp = Node1.next;
Node1.next = Node2.next;
Node2.next = temp;
if (Node1.next != null )
Node1.next.prev = Node1;
if (Node2.next != null )
Node2.next.prev = Node2;
temp = Node1.prev;
Node1.prev = Node2.prev;
Node2.prev = temp;
if (Node1.prev != null )
Node1.prev.next = Node1;
if (Node2.prev != null )
Node2.prev.next = Node2;
}
static void sort()
{
if (head == tail || head == null )
return ;
Node first = head;
Node last = tail;
while ((first != last)
&& (first.prev != last)) {
if (first.data == 0 )
first = first.next;
if (last.data == 1 )
last = last.prev;
if (first.data == 1 && last.data == 0
&& first.prev != last) {
swap(first, last);
if (head == first)
head = last;
if (tail == last)
tail = first;
Node Temp = first;
first = last.next;
last = Temp.prev;
}
}
}
public static void main(String[] args)
{
push( 1 );
push( 1 );
push( 0 );
push( 0 );
push( 1 );
push( 0 );
push( 1 );
push( 1 );
push( 0 );
push( 0 );
sort();
print(head);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
self .prev = None
class LinkedList:
def __init__( self ):
self .head = None
self .tail = None
def push( self , d):
if ( self .tail = = None ):
temp = Node(d);
self .head = temp;
self .tail = temp;
else :
temp = Node(d);
self .tail. next = temp
temp.prev = self .tail
self .tail = temp
def printList( self ):
curr = self .head
if ( self .head = = None ):
print ( "List is Empty" )
return
print (curr.data, " " , end = '')
curr = curr. next
while curr ! = None :
print (curr.data, " " , end = '')
curr = curr. next
print ( "\n" )
def swap( self , Node1, Node2):
temp = None
temp = Node1. next
Node1. next = Node2. next
Node2. next = temp
if (Node1. next ! = None ):
Node1. next .prev = Node1
if (Node2. next ! = None ):
Node2. next .prev = Node2
temp = Node1.prev
Node1.prev = Node2.prev
Node2.prev = temp
if (Node1.prev ! = None ):
Node1.prev. next = Node1
if (Node2.prev ! = None ):
Node2.prev. next = Node2
def sort( self ):
if ( self .head = = self .tail or self .head = = None ):
return ;
first = self .head
last = self .tail
while ((first ! = last) and (first.prev ! = last)):
if (first.data = = 0 ):
first = first. next
if (last.data = = 1 ):
last = last.prev
if (first.data = = 1 and last.data = = 0 and first.prev ! = last):
self .swap(first, last);
if ( self .head = = first):
self .head = last;
if ( self .tail = = last):
self .tail = first
Temp = first
first = last. next ;
last = Temp.prev;
if __name__ = = '__main__' :
llist = LinkedList()
llist.push( 1 )
llist.push( 1 )
llist.push( 0 )
llist.push( 0 )
llist.push( 1 )
llist.push( 0 )
llist.push( 1 )
llist.push( 1 )
llist.push( 0 )
llist.push( 0 )
llist.sort()
llist.printList()
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static Node head;
static Node tail;
public class Node {
public int data;
public Node prev;
public Node next;
public Node( int data)
{
this .data = data;
this .prev = null ;
this .next = null ;
}
};
static void print(Node head)
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data+ " " );
temp = temp.next;
}
Console.WriteLine();
}
static void push( int item)
{
if (tail == null ) {
Node temp = new Node(item);
tail = temp;
head = temp;
}
else {
Node temp = new Node(item);
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
static void swap(Node Node1, Node Node2)
{
Node temp;
temp = Node1.next;
Node1.next = Node2.next;
Node2.next = temp;
if (Node1.next != null )
Node1.next.prev = Node1;
if (Node2.next != null )
Node2.next.prev = Node2;
temp = Node1.prev;
Node1.prev = Node2.prev;
Node2.prev = temp;
if (Node1.prev != null )
Node1.prev.next = Node1;
if (Node2.prev != null )
Node2.prev.next = Node2;
}
static void sort()
{
if (head == tail || head == null )
return ;
Node first = head;
Node last = tail;
while ((first != last)
&& (first.prev != last)) {
if (first.data == 0)
first = first.next;
if (last.data == 1)
last = last.prev;
if (first.data == 1 && last.data == 0
&& first.prev != last) {
swap(first, last);
if (head == first)
head = last;
if (tail == last)
tail = first;
Node Temp = first;
first = last.next;
last = Temp.prev;
}
}
}
public static void Main(String[] args)
{
push(1);
push(1);
push(0);
push(0);
push(1);
push(0);
push(1);
push(1);
push(0);
push(0);
sort();
print(head);
}
}
|
Javascript
class Node{
constructor(data) {
this .data = data;
this .prev = null ;
this .next = null ;
}
}
let head;
let tail;
function print(){
let temp = head;
while (temp != null ){
document.write(temp.data);
temp = temp.next;
}
document.write();
}
function push(item){
if (tail == null ){
let temp = new Node(item);
tail = temp;
head = temp;
}
else {
let temp = new Node(item);
tail.next = temp;
temp.prev = tail;
tail = temp;
}
}
function swap(node1, node2){
let temp;
temp = node1.next;
node1.next = node2.next;
node2.next = temp;
if (node1.next != null )
node1.next.prev = node1;
if (node2.next != null )
node2.next.prev = node2;
temp = node1.prev;
node1.prev = node2.prev;
node2.prev = temp;
if (node1.prev != null )
node1.prev.next = node1;
if (node2.prev != null )
node2.prev.next = node2;
}
function sort(){
if (head == tail || head == null )
return ;
let first = head;
let last = tail;
while (first != last && first.prev != last){
if (first.data == 0)
first = first.next;
if (last.data == 1)
last = last.prev;
if (first.data == 1 && last.data == 0 && first.prev != last){
swap(first, last);
if (head == first)
head = last;
if (tail == last)
tail = first;
let Temp = first;
first = last.next;
last = Temp.prev;
}
}
}
push(1);
push(1);
push(0);
push(0);
push(1);
push(0);
push(1);
push(1);
push(0);
push(0);
sort();
print();
|
Output
0 0 0 0 0 1 1 1 1 1
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...