Insert value in sorted way in a sorted doubly linked list (original) (raw)
Last Updated : 04 Sep, 2024
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 using namespace std;
class Node { public: int data; Node* prev; Node* next;
Node(int new_data) {
data = new_data;
prev = nullptr;
next = nullptr;
}
};
// Function to insert element x into sorted DLL Node* sortedInsert(Node* head, int x) {
// Create a new node with the given data
Node* newNode = new Node(x);
// If the list is empty, set new node as the head
if (head == nullptr) {
return newNode;
}
// If new node needs to be inserted at beginning
if (x <= head->data) {
newNode->next = head;
head->prev = newNode;
return newNode;
}
// Traverse the list to find correct position
Node* curr = head;
while (curr->next != nullptr && curr->next->data < x) {
curr = curr->next;
}
// Insert the new node in the correct position
newNode->next = curr->next;
if (curr->next != nullptr) {
curr->next->prev = newNode;
}
curr->next = newNode;
newNode->prev = curr;
return head;
}
void printList(Node* curr) { while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } }
int main() {
// Create hardcoded DLL:
// 3 <-> 5 <-> 8 <-> 10 <-> 12
Node* head = new Node(3);
head->next = new Node(5);
head->next->prev = head;
head->next->next = new Node(8);
head->next->next->prev = head->next;
head->next->next->next = new Node(10);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = new Node(12);
head->next->next->next->next->prev =
head->next->next->next;
int x = 9;
head = sortedInsert(head, x);
printList(head);
return 0;
}
C
// C implementation to insert value in sorted way // in a sorted doubly linked list #include <stdio.h> #include <stdlib.h>
struct Node { int data; struct Node *prev, *next; };
struct Node *createNode(int data);
// Function to insert an element x into the correct // position in a sorted doubly linked list struct Node *sortedInsert(struct Node *head, int x) {
// Create a new node with the given data
struct Node *newNode = createNode(x);
// If the list is empty, return the new node
// as the head
if (head == NULL) {
return newNode;
}
// If the new node needs to be inserted at the
// beginning
if (x <= head->data) {
newNode->next = head;
head->prev = newNode;
return newNode;
}
// Traverse the list to find the correct position
// to insert the new node
struct Node *curr = head;
while (curr->next != NULL && curr->next->data < x) {
curr = curr->next;
}
// Insert the new node in the
// correct position
newNode->next = curr->next;
if (curr->next != NULL) {
curr->next->prev = newNode;
}
curr->next = newNode;
newNode->prev = curr;
return head;
}
void printList(struct Node *curr) { while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } }
struct Node *createNode(int data) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = data; newNode->prev = newNode->next = NULL; return newNode; }
int main() {
// Create a hardcoded doubly linked list:
// 3 <-> 5 <-> 8 <-> 10 <-> 12
struct Node *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;
int x = 9;
head = sortedInsert(head, x);
printList(head);
return 0;
}
Java
// Java implementation to insert value in sorted way // in a sorted doubly linked list class Node { int data; Node prev, next;
Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
public class GfG {
// Function to insert an element x into the
// correct position in a sorted doubly linked list
static Node sortedInsert(Node head, int x) {
// Create a new node with the given data
Node newNode = new Node(x);
// If the list is empty, return the new node
// as the head
if (head == null) {
return newNode;
}
// If the new node needs to be inserted at
// the beginning
if (x <= head.data) {
newNode.next = head;
head.prev = newNode;
return newNode;
}
// Traverse the list to find the correct
// position to insert the new node
Node curr = head;
while (curr.next != null && curr.next.data < x) {
curr = curr.next;
}
// Insert the new node in the correct position
newNode.next = curr.next;
if (curr.next != null) {
curr.next.prev = newNode;
}
curr.next = newNode;
newNode.prev = curr;
return head;
}
static void printList(Node curr) {
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 3 <-> 5 <-> 8 <-> 10 <-> 12
Node head = new Node(3);
head.next = new Node(5);
head.next.prev = head;
head.next.next = new Node(8);
head.next.next.prev = head.next;
head.next.next.next = new Node(10);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(12);
head.next.next.next.next.prev =
head.next.next.next;
int x = 9;
head = sortedInsert(head, x);
printList(head);
}
}
Python
Python implementation to insert value in sorted way
in a sorted doubly linked list
class Node: def init(self, data): self.data = data self.prev = None self.next = None
Function to insert an element x into the correct
position in a sorted doubly linked list
def sorted_insert(head, x):
# Create a new node with the given data
new_node = Node(x)
# If the list is empty, return the new node
# as the head
if head is None:
return new_node
# If the new node needs to be inserted at
# the beginning
if x <= head.data:
new_node.next = head
head.prev = new_node
return new_node
# Traverse the list to find the correct
# position to insert the new node
curr = head
while curr.next is not None and curr.next.data < x:
curr = curr.next
# Insert the new node in the correct position
new_node.next = curr.next
if curr.next is not None:
curr.next.prev = new_node
curr.next = new_node
new_node.prev = curr
return head
def print_list(curr): while curr is not None: print(curr.data, end=" ") curr = curr.next print()
if name == "main":
# Create a hardcoded doubly linked list:
# 3 <-> 5 <-> 8 <-> 10 <-> 12
head = Node(3)
head.next = Node(5)
head.next.prev = head
head.next.next = Node(8)
head.next.next.prev = head.next
head.next.next.next = Node(10)
head.next.next.next.prev = head.next.next
head.next.next.next.next = Node(12)
head.next.next.next.next.prev = head.next.next.next
x = 9
head = sorted_insert(head, x)
print_list(head)
C#
// C# implementation to insert value in sorted way // in a sorted doubly linked list using System;
class Node { public int data; public Node prev, next;
public Node(int data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
class GfG {
// Function to insert an element x into the
// correct position in a sorted doubly linked list
static Node SortedInsert(Node head, int x) {
// Create a new node with the given data
Node newNode = new Node(x);
// If the list is empty, return the new node
// as the head
if (head == null) {
return newNode;
}
// If the new node needs to be inserted at
// the beginning
if (x <= head.data) {
newNode.next = head;
head.prev = newNode;
return newNode;
}
// Traverse the list to find the correct
// position to insert the new node
Node curr = head;
while (curr.next != null && curr.next.data < x) {
curr = curr.next;
}
// Insert the new node in the correct position
newNode.next = curr.next;
if (curr.next != null) {
curr.next.prev = newNode;
}
curr.next = newNode;
newNode.prev = curr;
return head;
}
static void PrintList(Node curr) {
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 3 <-> 5 <-> 8 <-> 10 <-> 12
Node head = new Node(3);
head.next = new Node(5);
head.next.prev = head;
head.next.next = new Node(8);
head.next.next.prev = head.next;
head.next.next.next = new Node(10);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(12);
head.next.next.next.next.prev = head.next.next.next;
int x = 9;
head = SortedInsert(head, x);
PrintList(head);
}
}
JavaScript
// JavaScript implementation to insert value // in sorted way in a sorted doubly linked list
class Node { 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 list function sortedInsert(head, x) {
// Create a new node with the given data
const newNode = new Node(x);
// If the list is empty, return the new node
// as the head
if (head === null) {
return newNode;
}
// If the new node needs to be inserted at
// the beginning
if (x <= head.data) {
newNode.next = head;
head.prev = newNode;
return newNode;
}
// Traverse the list to find the correct
// position to insert the new node
let curr = head;
while (curr.next !== null && curr.next.data < x) {
curr = curr.next;
}
// Insert the new node in the correct position
newNode.next = curr.next;
if (curr.next !== null) {
curr.next.prev = newNode;
}
curr.next = newNode;
newNode.prev = curr;
return head;
}
function printList(curr) { while (curr !== null) { process.stdout.write(curr.data + " "); curr = curr.next; } console.log(); }
// Create a hardcoded doubly linked list: // 3 <-> 5 <-> 8 <-> 10 <-> 12 let head = new Node(3); head.next = new Node(5); head.next.prev = head; head.next.next = new Node(8); head.next.next.prev = head.next; head.next.next.next = new Node(10); head.next.next.next.prev = head.next.next; head.next.next.next.next = new Node(12); head.next.next.next.next.prev = head.next.next.next;
const x = 9; head = sortedInsert(head, x); printList(head);
`
**Time Complexity: O(n) , where **n is the number of nodes in the linked lsit.
**Auxiliary Space: O(1)