Bird
Raised Fist0
DSA Goprogramming~10 mins

Delete Node at End in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at End
Start at Head
↓
Check if list empty?
Yes→Exit: Nothing to delete
No↓
Traverse to second last node
↓
Set second last node's next to null
↓
Delete last node
↓
End
Start from the head, check if list is empty, then move to the second last node and remove the last node by updating pointers.
Execution Sample
DSA Go
func deleteAtEnd(head *Node) *Node {
  if head == nil {
    return nil
  }
  if head.next == nil {
    return nil
  }
  curr := head
  for curr.next.next != nil {
    curr = curr.next
  }
  curr.next = nil
  return head
}
Deletes the last node from a singly linked list and returns the updated head.
Execution Table
StepCurrent Node ValueConditionActionList State After Action
1head (1)head == nil? FalseCheck if list empty1 -> 2 -> 3 -> 4 -> null
2head (1)head.next == nil? FalseCheck if only one node1 -> 2 -> 3 -> 4 -> null
3curr = head (1)curr.next.next != nil? TrueTraverse to next node1 -> 2 -> 3 -> 4 -> null
4curr = 2curr.next.next != nil? TrueTraverse to next node1 -> 2 -> 3 -> 4 -> null
5curr = 3curr.next.next != nil? FalseStop traversal1 -> 2 -> 3 -> 4 -> null
6curr = 3Set curr.next = nilDelete last node (4)1 -> 2 -> 3 -> null
7Return headEndReturn updated list1 -> 2 -> 3 -> null
💡 Traversal stops when curr.next.next is nil, meaning curr is second last node.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
headNode(1)Node(1)Node(1)Node(1)Node(1)Node(1)
currnilNode(1)Node(2)Node(3)Node(3)Node(3)
Key Moments - 2 Insights
Why do we check curr.next.next != nil instead of curr.next != nil?
Because we want to stop at the second last node, so that we can set its next pointer to nil and remove the last node. This is shown in execution_table rows 3 to 5.
What happens if the list has only one node?
The function returns nil immediately after checking head.next == nil (row 2), meaning the list becomes empty after deletion.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of curr at step 4?
ANode with value 2
BNode with value 3
CNode with value 1
DNode with value 4
💡 Hint
Check the 'Current Node Value' column at step 4 in execution_table.
At which step does the traversal stop because curr.next.next is nil?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step where condition 'curr.next.next != nil' becomes False in execution_table.
If the list had only one node, what would the function return?
AThe node with next set to itself
BThe same single node
Cnil (empty list)
DAn error
💡 Hint
Refer to step 2 in execution_table where head.next == nil is checked.
Concept Snapshot
Delete Node at End in singly linked list:
- Check if list empty or single node
- Traverse to second last node
- Set second last node's next to nil
- Return updated head
- Handles empty and single-node lists safely
Full Transcript
This concept shows how to delete the last node from a singly linked list. We start by checking if the list is empty or has only one node. If empty, we return nil. If only one node, deleting it means returning nil as well. Otherwise, we traverse the list until we reach the second last node. We do this by checking if the next node's next is nil, which means the next node is the last. Then we set the second last node's next pointer to nil, effectively removing the last node. Finally, we return the head of the updated list. The execution table traces each step, showing the current node, conditions checked, actions taken, and the list state after each action. Variable tracker shows how the pointer 'curr' moves through the list. Key moments clarify why we check curr.next.next and what happens with single-node lists. The visual quiz tests understanding of these steps.

Practice

(1/5)
1. What does the operation Delete Node at End do in a linked list?
easy
A. Removes the last node from the linked list
B. Adds a new node at the end of the linked list
C. Removes the first node from the linked list
D. Deletes all nodes in the linked list

Solution

  1. Step 1: Understand the linked list structure

    A linked list is a chain of nodes where each node points to the next node.
  2. 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.
  3. Final Answer:

    Removes the last node from the linked list -> Option A
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Deleting at end always removes the tail node [OK]
Common Mistakes:
  • Confusing delete at end with delete at start
  • Thinking it adds a node instead of removing
  • Assuming it deletes all nodes
2. Which of the following Go code snippets correctly deletes the last node of a singly linked list?
easy
A. 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 }
B. func deleteEnd(head *Node) *Node { head = head.next; return head }
C. func deleteEnd(head *Node) *Node { head = nil; return head }
D. func deleteEnd(head *Node) *Node { curr := head; for curr.next != nil { curr = curr.next } curr = nil; return head }

Solution

  1. 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).
  2. 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.
  3. Final Answer:

    Code in option A correctly deletes the last node -> Option A
  4. Quick Check:

    Traverse to second last node, set next to nil [OK]
Hint: Stop at second last node, then remove last [OK]
Common Mistakes:
  • Setting head to nil incorrectly
  • Not stopping at second last node
  • Trying to delete node by setting local pointer nil
3. What will be the output linked list after running this Go code to delete the last node?
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")
}
medium
A. 2 -> 3 -> nil
B. 1 -> 2 -> 3 -> nil
C. 1 -> 2 -> nil
D. nil

Solution

  1. Step 1: Understand initial list

    List is 1 -> 2 -> 3 -> nil before deletion.
  2. Step 2: Delete last node

    deleteEnd removes node with value 3, so list becomes 1 -> 2 -> nil.
  3. Final Answer:

    1 -> 2 -> nil -> Option C
  4. Quick Check:

    Last node removed, list ends at 2 [OK]
Hint: Deleting end removes last value from printed list [OK]
Common Mistakes:
  • Forgetting to update next pointer to nil
  • Deleting first node instead of last
  • Printing old list without deletion
4. Find the bug in this Go function that tries to delete the last node of a singly linked list:
func deleteEnd(head *Node) *Node {
if head == nil { return nil }
curr := head
for curr.next != nil {
curr = curr.next
}
curr = nil
return head
}
medium
A. It does not handle empty list correctly
B. It causes a runtime panic due to nil pointer dereference
C. It deletes the entire list instead of last node
D. It does not update the second last node's next pointer

Solution

  1. Step 1: Analyze the loop

    The loop moves curr to the last node (where curr.next is nil).
  2. 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.
  3. Final Answer:

    It does not update the second last node's next pointer -> Option D
  4. Quick Check:

    Must update second last node's next to nil [OK]
Hint: Update second last node's next, not local pointer [OK]
Common Mistakes:
  • Setting local pointer nil instead of previous node's next
  • Not handling single node list
  • Assuming curr = nil deletes node
5. Given a singly linked list, how would you modify the deleteEnd function to handle the edge case when the list has only one node?
hard
A. Do nothing and return head as is
B. Return nil immediately if head.next is nil, indicating list becomes empty
C. Set head.next to nil and return head
D. Traverse the list twice to confirm single node

Solution

  1. Step 1: Identify single node case

    If head.next is nil, list has only one node.
  2. Step 2: Decide deletion result

    Deleting last node means list becomes empty, so return nil.
  3. Final Answer:

    Return nil immediately if head.next is nil, indicating list becomes empty -> Option B
  4. Quick Check:

    Single node deletion returns empty list [OK]
Hint: Check if only one node, then return nil [OK]
Common Mistakes:
  • Not handling single node case causing errors
  • Setting head.next to nil but returning non-empty list
  • Unnecessary extra traversal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard