Delete from Beginning of Doubly Linked List in DSA Go - Time & Space Complexity
We want to understand how long it takes to remove the first item from a doubly linked list.
How does the time needed change when the list grows bigger?
Analyze the time complexity of the following code snippet.
// Node represents a doubly linked list node
type Node struct {
data int
prev *Node
next *Node
}
// DeleteFromBeginning removes the first node
func DeleteFromBeginning(head **Node) {
if *head == nil {
return
}
*head = (*head).next
if *head != nil {
(*head).prev = nil
}
}
This code removes the first node from a doubly linked list by updating the head pointer and adjusting links.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Updating pointers of the first node and head reference.
- How many times: This happens only once per deletion, no loops or recursion.
Removing the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 5 |
| 100 | 5 |
| 1000 | 5 |
Pattern observation: The number of steps stays constant as the list grows.
Time Complexity: O(1)
This means deleting the first node takes the same amount of time no matter how large the list is.
[X] Wrong: "Deleting the first node takes longer if the list is bigger because we have to check all nodes."
[OK] Correct: We only change a few pointers at the start, so the list size does not affect the time.
Knowing that deleting from the start of a doubly linked list is quick helps you explain efficient list operations clearly.
"What if we changed to deleting from the end of the doubly linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer always points to the first node in the list.Step 2: Effect of deleting the first node
When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.Final Answer:
It moves to the second node in the list -> Option AQuick Check:
Head points to new first node after deletion [OK]
- Thinking head becomes null without checking list emptiness
- Assuming head points to last node after deletion
- Not updating head pointer after deletion
dll?Solution
Step 1: Move head to next node
Setdll.head = dll.head.nextto remove first node reference.Step 2: Update new head's prev pointer
If new head exists, setdll.head.prev = nilto detach backward link.Final Answer:
dll.head = dll.head.next; if dll.head != nil { dll.head.prev = nil } -> Option BQuick Check:
Correct pointer updates for deletion [OK]
- Setting prev pointer before moving head
- Not checking if new head is nil
- Clearing head and tail without condition
type Node struct {
data int
prev *Node
next *Node
}
head := &Node{data: 10}
second := &Node{data: 20}
third := &Node{data: 30}
head.next = second
second.prev = head
second.next = third
third.prev = second
// Delete from beginning
head = head.next
head.prev = nil
// Print list
curr := head
for curr != nil {
fmt.Print(curr.data)
if curr.next != nil {
fmt.Print(" <-> ")
}
curr = curr.next
}
Solution
Step 1: Initial list setup
List is 10 <-> 20 <-> 30 with head at 10.Step 2: Delete first node
Head moves to 20, and 20's prev is set to nil, detaching 10.Final Answer:
20 <-> 30 -> Option AQuick Check:
First node removed, list starts at 20 [OK]
- Not clearing prev pointer of new head
- Printing old head node
- Confusing next and prev pointers
if dll.head != nil {
dll.head = dll.head.next
dll.head.prev = nil
}Solution
Step 1: Check condition before accessing pointers
The code moves head to next node without checking if next node exists.Step 2: Accessing prev of nil causes runtime error
Ifdll.head.nextis nil, thendll.headbecomes nil and accessingdll.head.prevcauses panic.Final Answer:
It does not check if dll.head.next is nil before accessing prev -> Option DQuick Check:
Always check for nil before pointer access [OK]
- Assuming next node always exists
- Not handling empty list after deletion
- Confusing prev and next pointers
Solution
Step 1: Delete first node properly
Move head to next node and set new head's prev to nil to detach old first node.Step 2: Insert new node at beginning
Create new node with next pointing to current head and prev nil; update current head's prev to new node; update head pointer to new node.Final Answer:
Delete first node by moving head to next and clearing prev; then create new node with next = head, prev = nil; update head.prev to new node; finally set head to new node -> Option CQuick Check:
Proper delete then insert at head [OK]
- Not updating prev pointer of old head after insertion
- Setting new node's prev incorrectly
- Overwriting head without linking new node
Ready to prove it?
Pass the test and this pattern moves unknown
