Given a Sorted Doubly Linked Listin (non-decreasing order) and an element x, the task is to insert the element x into the correct position in the Sorted Doubly Linked List.
Example:
Input: LinkedList = 3<->5<->8<->10<->12 , x = 9 Output: 3<->5<->8<->9<->10<->12
Explanation: Here node 9 is inserted between 8 and 10 in the Doubly Linked-List.
Input: LinkedList = 1<->4<->10<->11 , x = 15 Output: 1<->4<->10<->11<->15
Explanation: Here node 15 is inserted at end in the Doubly Linked-List.
Approach:
To insert a value into a sorted doubly linked list while maintaining sorted order, start by creating a new node with the given value.
If the list is empty, new node becomes the head of the doubly linked list.
else If the new node's value is smaller than or equal to the head's value, insert it at the beginning.
else, traverse the list to find the correct position where the new node’s value is greater than the current node and smaller than the next node. Adjust pointers to insert the node between the current and next node or after the current node (if curr's next is NULL) .
C++
// C++ implementation to insert value in sorted way// in a sorted doubly linked list#include<iostream>usingnamespacestd;classNode{public:intdata;Node*prev;Node*next;Node(intnew_data){data=new_data;prev=nullptr;next=nullptr;}};// Function to insert element x into sorted DLLNode*sortedInsert(Node*head,intx){// Create a new node with the given dataNode*newNode=newNode(x);// If the list is empty, set new node as the headif(head==nullptr){returnnewNode;}// If new node needs to be inserted at beginningif(x<=head->data){newNode->next=head;head->prev=newNode;returnnewNode;}// Traverse the list to find correct positionNode*curr=head;while(curr->next!=nullptr&&curr->next->data<x){curr=curr->next;}// Insert the new node in the correct positionnewNode->next=curr->next;if(curr->next!=nullptr){curr->next->prev=newNode;}curr->next=newNode;newNode->prev=curr;returnhead;}voidprintList(Node*curr){while(curr!=nullptr){cout<<curr->data<<" ";curr=curr->next;}}intmain(){// Create hardcoded DLL: // 3 <-> 5 <-> 8 <-> 10 <-> 12Node*head=newNode(3);head->next=newNode(5);head->next->prev=head;head->next->next=newNode(8);head->next->next->prev=head->next;head->next->next->next=newNode(10);head->next->next->next->prev=head->next->next;head->next->next->next->next=newNode(12);head->next->next->next->next->prev=head->next->next->next;intx=9;head=sortedInsert(head,x);printList(head);return0;}
C
// C implementation to insert value in sorted way// in a sorted doubly linked list#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*prev,*next;};structNode*createNode(intdata);// Function to insert an element x into the correct// position in a sorted doubly linked liststructNode*sortedInsert(structNode*head,intx){// Create a new node with the given datastructNode*newNode=createNode(x);// If the list is empty, return the new node// as the headif(head==NULL){returnnewNode;}// If the new node needs to be inserted at the// beginningif(x<=head->data){newNode->next=head;head->prev=newNode;returnnewNode;}// Traverse the list to find the correct position// to insert the new nodestructNode*curr=head;while(curr->next!=NULL&&curr->next->data<x){curr=curr->next;}// Insert the new node in the // correct positionnewNode->next=curr->next;if(curr->next!=NULL){curr->next->prev=newNode;}curr->next=newNode;newNode->prev=curr;returnhead;}voidprintList(structNode*curr){while(curr!=NULL){printf("%d ",curr->data);curr=curr->next;}}structNode*createNode(intdata){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=data;newNode->prev=newNode->next=NULL;returnnewNode;}intmain(){// Create a hardcoded doubly linked list:// 3 <-> 5 <-> 8 <-> 10 <-> 12structNode*head=createNode(3);head->next=createNode(5);head->next->prev=head;head->next->next=createNode(8);head->next->next->prev=head->next;head->next->next->next=createNode(10);head->next->next->next->prev=head->next->next;head->next->next->next->next=createNode(12);head->next->next->next->next->prev=head->next->next->next;intx=9;head=sortedInsert(head,x);printList(head);return0;}
Java
// Java implementation to insert value in sorted way// in a sorted doubly linked listclassNode{intdata;Nodeprev,next;Node(intdata){this.data=data;this.prev=null;this.next=null;}}publicclassGfG{// Function to insert an element x into the// correct position in a sorted doubly linked liststaticNodesortedInsert(Nodehead,intx){// Create a new node with the given dataNodenewNode=newNode(x);// If the list is empty, return the new node// as the headif(head==null){returnnewNode;}// If the new node needs to be inserted at// the beginningif(x<=head.data){newNode.next=head;head.prev=newNode;returnnewNode;}// Traverse the list to find the correct// position to insert the new nodeNodecurr=head;while(curr.next!=null&&curr.next.data<x){curr=curr.next;}// Insert the new node in the correct positionnewNode.next=curr.next;if(curr.next!=null){curr.next.prev=newNode;}curr.next=newNode;newNode.prev=curr;returnhead;}staticvoidprintList(Nodecurr){while(curr!=null){System.out.print(curr.data+" ");curr=curr.next;}}publicstaticvoidmain(String[]args){// Create a hardcoded doubly linked list:// 3 <-> 5 <-> 8 <-> 10 <-> 12Nodehead=newNode(3);head.next=newNode(5);head.next.prev=head;head.next.next=newNode(8);head.next.next.prev=head.next;head.next.next.next=newNode(10);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(12);head.next.next.next.next.prev=head.next.next.next;intx=9;head=sortedInsert(head,x);printList(head);}}
Python
# Python implementation to insert value in sorted way# in a sorted doubly linked listclassNode:def__init__(self,data):self.data=dataself.prev=Noneself.next=None# Function to insert an element x into the correct# position in a sorted doubly linked listdefsorted_insert(head,x):# Create a new node with the given datanew_node=Node(x)# If the list is empty, return the new node# as the headifheadisNone:returnnew_node# If the new node needs to be inserted at# the beginningifx<=head.data:new_node.next=headhead.prev=new_nodereturnnew_node# Traverse the list to find the correct# position to insert the new nodecurr=headwhilecurr.nextisnotNoneandcurr.next.data<x:curr=curr.next# Insert the new node in the correct positionnew_node.next=curr.nextifcurr.nextisnotNone:curr.next.prev=new_nodecurr.next=new_nodenew_node.prev=currreturnheaddefprint_list(curr):whilecurrisnotNone:print(curr.data,end=" ")curr=curr.nextprint()if__name__=="__main__":# Create a hardcoded doubly linked list:# 3 <-> 5 <-> 8 <-> 10 <-> 12head=Node(3)head.next=Node(5)head.next.prev=headhead.next.next=Node(8)head.next.next.prev=head.nexthead.next.next.next=Node(10)head.next.next.next.prev=head.next.nexthead.next.next.next.next=Node(12)head.next.next.next.next.prev=head.next.next.nextx=9head=sorted_insert(head,x)print_list(head)
C#
// C# implementation to insert value in sorted way// in a sorted doubly linked listusingSystem;classNode{publicintdata;publicNodeprev,next;publicNode(intdata){this.data=data;this.prev=null;this.next=null;}}classGfG{// Function to insert an element x into the// correct position in a sorted doubly linked liststaticNodeSortedInsert(Nodehead,intx){// Create a new node with the given dataNodenewNode=newNode(x);// If the list is empty, return the new node// as the headif(head==null){returnnewNode;}// If the new node needs to be inserted at// the beginningif(x<=head.data){newNode.next=head;head.prev=newNode;returnnewNode;}// Traverse the list to find the correct// position to insert the new nodeNodecurr=head;while(curr.next!=null&&curr.next.data<x){curr=curr.next;}// Insert the new node in the correct positionnewNode.next=curr.next;if(curr.next!=null){curr.next.prev=newNode;}curr.next=newNode;newNode.prev=curr;returnhead;}staticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(curr.data+" ");curr=curr.next;}Console.WriteLine();}staticvoidMain(){// Create a hardcoded doubly linked list:// 3 <-> 5 <-> 8 <-> 10 <-> 12Nodehead=newNode(3);head.next=newNode(5);head.next.prev=head;head.next.next=newNode(8);head.next.next.prev=head.next;head.next.next.next=newNode(10);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(12);head.next.next.next.next.prev=head.next.next.next;intx=9;head=SortedInsert(head,x);PrintList(head);}}
JavaScript
// JavaScript implementation to insert value// in sorted way in a sorted doubly linked listclassNode{constructor(data){this.data=data;this.prev=null;this.next=null;}}// Function to insert an element x into the correct// position in a sorted doubly linked listfunctionsortedInsert(head,x){// Create a new node with the given dataconstnewNode=newNode(x);// If the list is empty, return the new node// as the headif(head===null){returnnewNode;}// If the new node needs to be inserted at// the beginningif(x<=head.data){newNode.next=head;head.prev=newNode;returnnewNode;}// Traverse the list to find the correct// position to insert the new nodeletcurr=head;while(curr.next!==null&&curr.next.data<x){curr=curr.next;}// Insert the new node in the correct positionnewNode.next=curr.next;if(curr.next!==null){curr.next.prev=newNode;}curr.next=newNode;newNode.prev=curr;returnhead;}functionprintList(curr){while(curr!==null){process.stdout.write(curr.data+" ");curr=curr.next;}console.log();}// Create a hardcoded doubly linked list:// 3 <-> 5 <-> 8 <-> 10 <-> 12lethead=newNode(3);head.next=newNode(5);head.next.prev=head;head.next.next=newNode(8);head.next.next.prev=head.next;head.next.next.next=newNode(10);head.next.next.next.prev=head.next.next;head.next.next.next.next=newNode(12);head.next.next.next.next.prev=head.next.next.next;constx=9;head=sortedInsert(head,x);printList(head);
Output
3 5 8 9 10 12
Time Complexity: O(n) , where n is the number of nodes in the linked lsit. Auxiliary Space: O(1)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.