Technical Interview Question:
You are given a sorted circular doubly linked list and a value to insert. Write a function to insert the value into its correct position in the linked list, while keeping the list sorted.
The circular doubly linked list is represented by a pointer to the head (the first node) of the list, and each node contains a value and pointers to its previous and next nodes. The last node in the list also points to the head node, making it circular.
Implement the function insertIntoSortedCircularLL(Node head, int value)
which takes the head of the list and the value to insert, and returns the new head of the modified list.
Example:
Input: Head -> 1 <-> 3 <-> 4 <-> 6 (circular)
Value to insert: 5
Output: Head -> 1 <-> 3 <-> 4 <-> 5 <-> 6 (circular)
Answer:
To solve this problem, we can follow these steps:
Check if the given list is empty. If it is, create a new node with the given value and make it the head by pointing its next
and prev
pointers to itself. Return this new node as the head of the modified list.
Initialize a variable current
to point to the head node of the list.
Traverse the list until we find a node whose value is greater than the given value or until we reach the head node again. This will help us determine the correct position to insert the new node.
Once we find the correct position, create a new node with the given value.
Handle the connections of the new node with the existing nodes:
prev
pointer to the node preceding the correct position.next
pointer to the node at the correct position.prev
pointer of the node at the correct position to point to the new node.next
pointer of the node preceding the correct position to point to the new node.If the correct position is the head node (i.e., the given value is smaller than all the values in the list), update the head pointer to the new node.
Return the head of the modified list.
Here is the implementation in Java:
class Node {
int data;
Node prev;
Node next;
public Node(int data) {
this.data = data;
}
}
public class DoublyLinkedList {
public static Node insertIntoSortedCircularLL(Node head, int value) {
if (head == null) {
// If list is empty, create a new node with given value as head
Node newNode = new Node(value);
newNode.prev = newNode;
newNode.next = newNode;
return newNode;
}
Node current = head;
while (current.next != head && current.next.data < value) {
current = current.next;
}
Node newNode = new Node(value);
newNode.prev = current;
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
if (value < head.data) {
// If the correct position is the head node
head = newNode;
}
return head;
}
public static void main(String[] args) {
Node head = new Node(1);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(6);
head.next = node2;
node2.prev = head;
node2.next = node3;
node3.prev = node2;
node3.next = node4;
node4.prev = node3;
node4.next = head;
head.prev = node4;
int valueToInsert = 5;
head = insertIntoSortedCircularLL(head, valueToInsert);
// Print the modified list
Node current = head;
do {
System.out.print(current.data + " <-> ");
current = current.next;
} while (current != head);
System.out.println(head.data); // Print the head data to complete the circular list
}
}
The output will be: 1 <-> 3 <-> 4 <-> 5 <-> 6 <-> 1