Delete Node at Beginning in DSA Go - Time & Space Complexity
We want to know how the time to delete the first node in a linked list changes as the list grows.
How much work does it take to remove the first item, no matter the list size?
Analyze the time complexity of the following code snippet.
// Delete the first node from a singly linked list
func deleteAtBeginning(head *Node) *Node {
if head == nil {
return nil
}
return head.Next
}
This code removes the first node by returning the second node as the new head.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the first node and its next pointer once.
- How many times: Exactly one time, no loops or recursion.
Deleting the first node always takes the same steps, no matter how big the list is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work 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 many nodes are in the list.
[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 the head pointer to the next node, so we do not look at other nodes at all.
Understanding this simple operation helps build confidence with linked lists and shows you can reason about efficient updates.
"What if we had to delete the last node instead? How would the time complexity change?"
Practice
Solution
Step 1: Understand the head pointer role
The head pointer points to the first node in the list.Step 2: Effect of deleting the first node
Deleting the first node means moving the head pointer to the next node, which becomes the new first node.Final Answer:
The head pointer moves to the second node -> Option AQuick Check:
Deleting first node updates head = next node [OK]
- Thinking last node is deleted
- Assuming list becomes empty always
- Deleting middle node by mistake
head?Solution
Step 1: Understand pointer update to delete first node
To delete the first node, head must point to the second node, which ishead.next.Step 2: Check each option for correctness
head = head.next correctly assignshead = head.next. Others either assign incorrectly or cause errors.Final Answer:
head = head.next -> Option DQuick Check:
head = head.next deletes first node [OK]
- Assigning head.next to head (wrong direction)
- Using nil.next which causes error
- Setting head.next to nil deletes wrong node
package main
import "fmt"
type Node struct {
value int
next *Node
}
func main() {
head := &Node{value: 1}
head.next = &Node{value: 2}
head.next.next = &Node{value: 3}
// Delete first node
head = head.next
// Print list
current := head
for current != nil {
fmt.Print(current.value, " -> ")
current = current.next
}
fmt.Print("null")
}Solution
Step 1: Initial list nodes and values
The list starts as 1 -> 2 -> 3 -> null.Step 2: Deleting first node by head = head.next
Head moves from node with value 1 to node with value 2, so list becomes 2 -> 3 -> null.Final Answer:
2 -> 3 -> null -> Option AQuick Check:
After deletion, list starts at second node [OK]
- Printing original list without deletion
- Assuming first node still prints
- Confusing null with empty list
package main
type Node struct {
value int
next *Node
}
func main() {
var head *Node = nil
// Delete first node
head = head.next
}Solution
Step 1: Identify cause of runtime panic
head is nil, so accessing head.next causes nil pointer dereference panic.Step 2: Fix by checking if head is nil before deleting
Check if head != nil before assigning head = head.next to avoid panic.Final Answer:
Runtime panic: nil pointer dereference; fix by checking if head is nil before deletion -> Option BQuick Check:
Nil check prevents runtime panic [OK]
- Ignoring nil check causing panic
- Assuming head is never nil
- Confusing syntax and runtime errors
func deleteFirstNode(head *Node) *Node {
if head == nil {
return nil
}
head = head.next
return head
}Solution
Step 1: Understand function logic
If head is nil, return nil. Otherwise, move head to head.next and return it.Step 2: Apply function to list 10 -> 20 -> 30 -> 40 -> null
Deleting first node (10) moves head to node with value 20, so list becomes 20 -> 30 -> 40 -> null.Final Answer:
20 -> 30 -> 40 -> null -> Option CQuick Check:
Function returns list starting from second node [OK]
- Returning original head without deletion
- Not handling nil head case
- Deleting wrong node by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
