Why Doubly Linked List Over Singly Linked List in DSA Go - Performance Analysis
We want to understand how using a doubly linked list changes the time it takes to do common operations compared to a singly linked list.
Which operations become faster or slower, and why?
Analyze the time complexity of inserting and deleting nodes in a doubly linked list.
// InsertAfter inserts newNode after node in doubly linked list
func InsertAfter(node, newNode *Node) {
newNode.prev = node
newNode.next = node.next
if node.next != nil {
node.next.prev = newNode
}
node.next = newNode
}
// DeleteNode removes node from doubly linked list
func DeleteNode(node *Node) {
if node.prev != nil {
node.prev.next = node.next
}
if node.next != nil {
node.next.prev = node.prev
}
}
This code inserts or deletes a node in a doubly linked list using pointers to previous and next nodes.
Look at the steps repeated during insertion and deletion.
- Primary operation: Updating pointers (prev and next) of adjacent nodes.
- How many times: A fixed number of pointer changes per operation, no loops involved.
Operations like insert or delete take the same number of steps no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 4 pointer updates |
| 100 | 4 pointer updates |
| 1000 | 4 pointer updates |
Pattern observation: The number of steps stays the same as the list grows.
Time Complexity: O(1)
This means insertion and deletion at a known position happen in constant time, regardless of list size.
[X] Wrong: "Doubly linked lists always take longer because they have more pointers to update."
[OK] Correct: Although there are more pointers, these updates happen a fixed number of times, so the time does not grow with list size.
Understanding when doubly linked lists give faster operations helps you choose the right data structure in real projects and interviews.
What if we only had a singly linked list and wanted to delete a node given only a pointer to it? How would the time complexity change?
Practice
doubly linked list over a singly linked list in a program?Solution
Step 1: Understand traversal capabilities
A doubly linked list stores two pointers per node: one to the next node and one to the previous node, allowing movement forward and backward.Step 2: Compare with singly linked list
A singly linked list stores only one pointer to the next node, so it can only be traversed forward easily.Final Answer:
Because it allows traversal in both directions easily -> Option DQuick Check:
Traversal both ways = B [OK]
- Thinking doubly linked lists use less memory
- Assuming singly linked lists allow backward traversal
- Confusing simplicity of implementation
Solution
Step 1: Identify required pointers for doubly linked list node
A doubly linked list node must have pointers to both previous and next nodes, so it needs two pointers of type *Node.Step 2: Check each option's struct fields
type Node struct { data int; prev *Node; next *Node } correctly declares bothprev *Nodeandnext *Node. Others miss one or use wrong types.Final Answer:
type Node struct { data int; prev *Node; next *Node } -> Option AQuick Check:
Two pointers prev and next = A [OK]
- Using int instead of pointer for prev/next
- Missing prev pointer in struct
- Confusing singly linked list node with doubly linked
type Node struct {
data int
prev *Node
next *Node
}
func printForward(head *Node) {
for current := head; current != nil; current = current.next {
fmt.Print(current.data, " ")
}
}
func printBackward(tail *Node) {
for current := tail; current != nil; current = current.prev {
fmt.Print(current.data, " ")
}
}
func main() {
n1 := &Node{data: 1}
n2 := &Node{data: 2}
n3 := &Node{data: 3}
n1.next = n2
n2.prev = n1
n2.next = n3
n3.prev = n2
printForward(n1)
fmt.Println()
printBackward(n3)
}Solution
Step 1: Trace printForward function
Starting at n1 (data=1), it prints 1, then moves to n2 (2), then n3 (3), then nil. Output: "1 2 3 "Step 2: Trace printBackward function
Starting at n3 (3), it prints 3, then moves to n2 (2), then n1 (1), then nil. Output: "3 2 1 "Final Answer:
1 2 3 \n 3 2 1 -> Option CQuick Check:
Forward then backward print = A [OK]
- Confusing prev and next pointers in loops
- Printing backward list in forward order
- Missing nil check causing runtime error
func insertAfter(node *Node, newNode *Node) {
newNode.next = node.next
node.next = newNode
newNode.prev = node
}Solution
Step 1: Analyze pointer updates in insertAfter
The code sets newNode.next to node.next and newNode.prev to node, and updates node.next to newNode.Step 2: Identify missing pointer update
If node.next was not nil, the previous pointer of node.next (the node after newNode) should be updated to newNode. This step is missing.Final Answer:
It does not update the prev pointer of the node after newNode -> Option BQuick Check:
Missing prev update of next node = C [OK]
- Forgetting to update next node's prev pointer
- Confusing prev and next pointers
- Assuming node.next is always nil
Solution
Step 1: Understand backward traversal requirement
Backward traversal means moving from a node to its previous node, which singly linked lists cannot do easily because they only have next pointers.Step 2: Understand deletion from middle nodes
To delete a node, you need to update the previous node's next pointer. Doubly linked lists have direct access to previous nodes, making deletion easier and efficient.Final Answer:
Because doubly linked lists allow backward traversal and easy access to previous nodes for deletion -> Option AQuick Check:
Backward traversal and deletion ease = D [OK]
- Thinking singly linked lists can traverse backward easily
- Ignoring need to update previous node on deletion
- Assuming doubly linked lists use less memory
Ready to prove it?
Pass the test and this pattern moves unknown
