Open In App

Insert value in sorted way in a sorted doubly linked list

Last Updated : 04 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a Sorted Doubly Linked List in (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

Insert-in-Sorted-way-in-a-Sorted-DLL-2

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

Insert-in-Sorted-way-in-a-Sorted-DLL-4

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>
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 Java Python C# JavaScript

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)



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg