Delete Node at End in DSA Go - Time & Space Complexity
We want to understand how long it takes to remove the last node from a linked list.
How does the time needed change as the list gets bigger?
Analyze the time complexity of the following code snippet.
func deleteAtEnd(head *Node) *Node {
if head == nil {
return nil
}
if head.next == nil {
return nil
}
current := head
for current.next.next != nil {
current = current.next
}
current.next = nil
return head
}
This code removes the last node from a singly linked list by walking to the second last node and cutting off the last.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for loop that moves through the list nodes.
- How many times: It runs once for each node until the second last node.
As the list gets longer, the loop runs more times to reach the second last node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 steps |
| 100 | About 99 steps |
| 1000 | About 999 steps |
Pattern observation: The number of steps grows roughly the same as the list size.
Time Complexity: O(n)
This means the time to delete the last node grows linearly with the list size.
[X] Wrong: "Deleting the last node is always quick because it's at the end."
[OK] Correct: We must walk through the list to find the second last node, so time grows with list length.
Understanding this helps you explain how linked lists work and shows you can analyze simple loops well.
"What if we kept a pointer to the last node? How would the time complexity change?"
Practice
Delete Node at End do in a linked list?Solution
Step 1: Understand the linked list structure
A linked list is a chain of nodes where each node points to the next node.Step 2: Identify the effect of deleting the last node
Deleting the last node means removing the node that has no next node, effectively shortening the list by one at the end.Final Answer:
Removes the last node from the linked list -> Option AQuick Check:
Delete Node at End = Remove last node [OK]
- Confusing delete at end with delete at start
- Thinking it adds a node instead of removing
- Assuming it deletes all nodes
Solution
Step 1: Check for empty or single node list
If list is empty or has one node, deleting last node results in empty list (return nil).Step 2: Traverse to second last node and remove last
Loop until curr.next.next is nil, then set curr.next to nil to remove last node.Final Answer:
Code in option A correctly deletes the last node -> Option AQuick Check:
Traverse to second last node, set next to nil [OK]
- Setting head to nil incorrectly
- Not stopping at second last node
- Trying to delete node by setting local pointer nil
type Node struct { val int; next *Node }
func main() {
head := &Node{val: 1, next: &Node{val: 2, next: &Node{val: 3, next: nil}}}
head = deleteEnd(head)
printList(head)
}
func deleteEnd(head *Node) *Node {
if head == nil || head.next == nil { return nil }
curr := head
for curr.next.next != nil { curr = curr.next }
curr.next = nil
return head
}
func printList(head *Node) {
for head != nil {
fmt.Print(head.val, " -> ")
head = head.next
}
fmt.Println("nil")
}Solution
Step 1: Understand initial list
List is 1 -> 2 -> 3 -> nil before deletion.Step 2: Delete last node
deleteEnd removes node with value 3, so list becomes 1 -> 2 -> nil.Final Answer:
1 -> 2 -> nil -> Option CQuick Check:
Last node removed, list ends at 2 [OK]
- Forgetting to update next pointer to nil
- Deleting first node instead of last
- Printing old list without deletion
func deleteEnd(head *Node) *Node {
if head == nil { return nil }
curr := head
for curr.next != nil {
curr = curr.next
}
curr = nil
return head
}Solution
Step 1: Analyze the loop
The loop moves curr to the last node (where curr.next is nil).Step 2: Check deletion logic
Setting curr = nil only changes local pointer, does not update previous node's next pointer, so last node remains linked.Final Answer:
It does not update the second last node's next pointer -> Option DQuick Check:
Must update second last node's next to nil [OK]
- Setting local pointer nil instead of previous node's next
- Not handling single node list
- Assuming curr = nil deletes node
deleteEnd function to handle the edge case when the list has only one node?Solution
Step 1: Identify single node case
If head.next is nil, list has only one node.Step 2: Decide deletion result
Deleting last node means list becomes empty, so return nil.Final Answer:
Return nil immediately if head.next is nil, indicating list becomes empty -> Option BQuick Check:
Single node deletion returns empty list [OK]
- Not handling single node case causing errors
- Setting head.next to nil but returning non-empty list
- Unnecessary extra traversal
Ready to prove it?
Pass the test and this pattern moves unknown
