Introduction and Basics of Doubly Linked List
A Doubly Linked List (DLL) is a linear data structure where each node stores data along with two pointers. One pointer points to the previous node, while the other points to the next node. This two-way connection allows traversal in both forward and backward directions.
Each node contains:
Data → Stores the value.
Prev Pointer → Points to the previous node.
Next Pointer → Points to the next node.
Example:
NULL ← 10 ⇄ 20 ⇄ 30 ⇄ 40 → NULL
Here, 10 is the head node and 40 is the tail node.
1. Convert Array to Doubly Linked List
Given an array of integers, create a Doubly Linked List containing the same elements in the same order. Every node should maintain links to both its previous and next nodes.
Example 1
Input: [10, 20, 30, 40]
Output: NULL ← 10 ⇄ 20 ⇄ 30 ⇄ 40 → NULL
Explanation: Each array element becomes a node, and adjacent nodes are connected using both previous and next pointers.
Example 2
Input: [5]
Output: NULL ← 5 → NULL
Explanation: A single element creates a DLL containing only one node with both pointers set to NULL.
Algorithm
Check whether the array is empty because no linked list can be created without elements.
Create the first node using the first array element and mark it as the head node of the DLL.
Maintain a pointer to the most recently created node because future nodes must be connected with it.
Traverse the remaining array elements one by one and create a new node for each value.
Connect the new node with the previous node by updating both the
nextandprevpointers.Move the tracking pointer to the newly created node and continue until all array elements are processed.
Time Complexity: O(N) because every array element is used exactly once to create a DLL node.
Space Complexity: O(N) because N nodes are created for the resulting Doubly Linked List.
2. Insert Before Head
Given a Doubly Linked List and a value X, insert a new node before the current head node and make it the new head of the list.
Example 1
Input: 10 ⇄ 20 ⇄ 30, X = 5
Output: 5 ⇄ 10 ⇄ 20 ⇄ 30
Explanation: The new node is inserted before the existing head and becomes the first node of the DLL.
Example 2
Input: NULL, X = 7
Output: 7
Explanation: Since the DLL is empty, the new node itself becomes the head.
Algorithm
Create a new node containing the value
X.Check whether the DLL is empty because the new node becomes the head if no nodes exist.
Connect the new node's next pointer to the current head node.
Update the current head node's previous pointer so that it points back to the new node.
Move the head pointer to the newly created node because it is now the first node.
Return the updated head pointer.
Time Complexity: O(1) because only a fixed number of pointer updates are performed.
Space Complexity: O(1) auxiliary space because no additional data structure is used.
3. Insert Before Tail
Given a Doubly Linked List and a value X, insert a new node immediately before the tail node.
Example 1
Input: 10 ⇄ 20 ⇄ 40, X = 30
Output: 10 ⇄ 20 ⇄ 30 ⇄ 40
Explanation: The new node is inserted just before the tail node containing value 40.
Example 2
Input: 10 ⇄ 20, X = 15
Output: 10 ⇄ 15 ⇄ 20
Explanation: The node is inserted before the last node while maintaining both forward and backward links.
Algorithm
Check whether the DLL contains fewer than two nodes because special handling may be required.
Traverse the DLL until the tail node is reached.
Create a new node containing the given value.
Locate the node immediately before the tail because insertion will happen between these two nodes.
Update the previous and next pointers of all affected nodes so that the new node becomes part of the chain.
Return the head pointer of the updated DLL.
Time Complexity: O(N) because reaching the tail may require traversing the list.
Space Complexity: O(1) because only a few pointers are maintained.
4. Insert Before Kth Node
Given a Doubly Linked List, a position K, and a value X, insert a new node immediately before the Kth node.
Example 1
Input: 10 ⇄ 20 ⇄ 40, K = 3, X = 30
Output: 10 ⇄ 20 ⇄ 30 ⇄ 40
Explanation: The new node is inserted before the node currently present at position three.
Example 2
Input: 10 ⇄ 20 ⇄ 30, K = 1, X = 5
Output: 5 ⇄ 10 ⇄ 20 ⇄ 30
Explanation: Inserting before the first position is equivalent to inserting before the head.
Algorithm
Check whether
Kequals1because insertion before the first node is a head insertion operation.Traverse the DLL until the Kth node is reached.
Create a new node containing the value that needs to be inserted.
Connect the new node with the node before the Kth node using both previous and next pointers.
Connect the new node with the Kth node so that all links remain valid.
Return the updated head pointer after insertion.
Time Complexity: O(N) because locating the Kth node may require traversal.
Space Complexity: O(1) because only constant extra memory is used.
5. Insert Before Given Node
Given a pointer to a node in a Doubly Linked List and a value X, insert a new node immediately before that node.
Example 1
Input: 10 ⇄ 20 ⇄ 40, Given Node = 40, X = 30
Output: 10 ⇄ 20 ⇄ 30 ⇄ 40
Explanation: The new node is inserted directly before the provided node.
Example 2
Input: 10 ⇄ 20 ⇄ 30, Given Node = 20, X = 15
Output: 10 ⇄ 15 ⇄ 20 ⇄ 30
Explanation: Since the target node is already known, insertion can be performed without searching.
Algorithm
Create a new node containing the value
X.Identify the node that exists immediately before the given node.
Connect the new node with the previous node through both pointer directions.
Connect the new node with the given node through both pointer directions.
Update the surrounding node pointers so the DLL remains connected.
Return the head pointer if required by the problem.
Time Complexity: O(1) because the target node is already provided.
Space Complexity: O(1) because only pointer modifications are performed.
6. Delete Head Node
Given a Doubly Linked List, delete the first node and return the updated head.
Example 1
Input: 10 ⇄ 20 ⇄ 30
Output: 20 ⇄ 30
Explanation: The first node is removed and the second node becomes the new head.
Example 2
Input: 5
Output: NULL
Explanation: Removing the only node makes the DLL empty.
Algorithm
Check whether the DLL is empty because deletion cannot be performed without nodes.
Store the current head node temporarily because it will be removed.
Move the head pointer to the next node in the DLL.
If a new head exists, set its previous pointer to
NULLbecause it becomes the first node.Delete the old head node safely from memory.
Return the updated head pointer.
Time Complexity: O(1) because deletion occurs directly at the beginning of the DLL.
Space Complexity: O(1) because only temporary pointers are used.
7. Delete Tail Node
Given a Doubly Linked List, remove the last node and return the modified list.
Example 1
Input: 10 ⇄ 20 ⇄ 30 ⇄ 40
Output: 10 ⇄ 20 ⇄ 30
Explanation: The last node is removed and the previous node becomes the new tail.
Example 2
Input: 5
Output: NULL
Explanation: Deleting the only node leaves the DLL empty.
Algorithm
Check whether the DLL is empty because there is nothing to delete.
If only one node exists, delete it and return
NULL.Traverse the DLL until the tail node is reached.
Identify the node immediately before the tail because it will become the new tail.
Disconnect the current tail node by updating both pointer links.
Delete the tail node and return the head pointer.
Time Complexity: O(N) because reaching the tail may require traversal.
Space Complexity: O(1) because only temporary pointers are maintained.
8. Delete Kth Node
Given a Doubly Linked List and an integer K, delete the node present at position K.
Example 1
Input: 10 ⇄ 20 ⇄ 30 ⇄ 40, K = 3
Output: 10 ⇄ 20 ⇄ 40
Explanation: The node at the third position is removed and the surrounding nodes are connected together.
Example 2
Input: 10 ⇄ 20 ⇄ 30, K = 1
Output: 20 ⇄ 30
Explanation: Deleting the first position is equivalent to deleting the head node.
Algorithm
Check whether the DLL is empty because no deletion can be performed.
If
Kequals1, perform head deletion directly.Traverse the DLL until the Kth node is reached.
Identify both the previous and next nodes surrounding the Kth node.
Connect the surrounding nodes directly using both pointer directions.
Delete the Kth node and return the updated head pointer.
Time Complexity: O(N) because locating the Kth node may require traversal.
Space Complexity: O(1) because only a few pointers are used.
9. Delete Given Node
Given a pointer to a node in a Doubly Linked List, delete that node while maintaining all valid DLL connections.
Example 1
Input: 10 ⇄ 20 ⇄ 30 ⇄ 40, Given Node = 30
Output: 10 ⇄ 20 ⇄ 40
Explanation: The target node is removed and its neighboring nodes are connected directly.
Example 2
Input: 10 ⇄ 20 ⇄ 30, Given Node = 20
Output: 10 ⇄ 30
Explanation: Since the node is already provided, no traversal is needed before deletion.
Algorithm
Identify the previous node and next node connected to the given node.
Check whether either side is missing because the given node may be the head or tail.
Connect the previous node directly to the next node by updating the next pointer.
Connect the next node back to the previous node by updating the previous pointer.
Remove the given node safely from memory.
Ensure all neighboring links remain valid after deletion.
Time Complexity: O(1) because the target node is directly available.
Space Complexity: O(1) because only pointer modifications are performed.
Be the first to add a comment.