Bird
Raised Fist0
DSA Goprogramming~10 mins

Delete a Node from Circular Linked List in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete a Node from Circular Linked List
Start at head
↓
Check if list empty?
Yes→Stop: Nothing to delete
No↓
Traverse nodes to find target
↓
Found node to delete?
No→Stop: Node not found
Yes↓
Adjust pointers to remove node
↓
If deleting head, update head pointer
↓
If only one node, list becomes empty
↓
Deletion complete
Start from head, check if list is empty, traverse to find node, adjust pointers to remove it, update head if needed, handle single-node case.
Execution Sample
DSA Go
func deleteNode(head *Node, key int) *Node {
 if head == nil {
  return nil
 }
 curr := head
 prev := head
 // Find node to delete
 for curr.data != key {
  prev = curr
  curr = curr.next
  if curr == head {
   return head
  }
 }
 // Single node case
 if curr == prev {
  head = nil
 } else {
  prev.next = curr.next
  if curr == head {
   head = curr.next
  }
 }
 return head
}
Deletes a node with given key from circular linked list, returns new head.
Execution Table
StepOperationCurrent Node DataPrev Node DataPointer ChangesVisual State
1Check if list emptynilnilNoneList: 1 -> 2 -> 3 -> 1 (head=1)
2Start traversal11NoneList: 1 -> 2 -> 3 -> 1 (head=1)
3Check if current.data == key (3)11No matchList: 1 -> 2 -> 3 -> 1 (head=1)
4Move prev and curr21NoneList: 1 -> 2 -> 3 -> 1 (head=1)
5Check if current.data == key (3)21No matchList: 1 -> 2 -> 3 -> 1 (head=1)
6Move prev and curr32NoneList: 1 -> 2 -> 3 -> 1 (head=1)
7Check if current.data == key (3)32Match foundList: 1 -> 2 -> 3 -> 1 (head=1)
8Check if single node32NoList: 1 -> 2 -> 3 -> 1 (head=1)
9Adjust prev.next to curr.next322.next = 1List: 1 -> 2 -> 1 (head=1)
10Check if deleting head32NoList: 1 -> 2 -> 1 (head=1)
11Return head12NoneList: 1 -> 2 -> 1 (head=1)
💡 Node with data 3 found and deleted; list updated with 3 removed.
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 9Final
headNode(1)Node(1)Node(1)Node(1)Node(1)
currNode(1)Node(1)Node(3)Node(3)Node(3)
prevNode(1)Node(1)Node(2)Node(2)Node(2)
List Size3 nodes3 nodes3 nodes2 nodes2 nodes
Key Moments - 3 Insights
Why do we check if curr == head during traversal?
Because the list is circular, if we come back to head without finding the key, the node is not present. See execution_table step 6 where curr moves and step 7 where match is checked.
What happens if the node to delete is the only node in the list?
We set head to nil, making the list empty. This is checked in execution_table step 8.
Why do we update head when deleting the head node?
Because head points to the first node, deleting it means head must point to the next node to keep the list accessible. See execution_table step 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'curr' at step 6?
ANode with data 3
BNode with data 1
CNode with data 2
Dnil
💡 Hint
Check the 'Current Node Data' column at step 6 in execution_table.
At which step does the pointer change that removes the node happen?
AStep 7
BStep 9
CStep 10
DStep 11
💡 Hint
Look for 'Adjust prev.next to curr.next' operation in execution_table.
If the node to delete was the head node, which step would update the head pointer?
AStep 10
BStep 9
CStep 8
DStep 11
💡 Hint
See the 'Check if deleting head' operation in execution_table.
Concept Snapshot
Delete Node in Circular Linked List:
- Start at head, check empty list
- Traverse until node with key found or back to head
- If single node and matches, list becomes empty
- Else, adjust prev.next to skip node
- If deleting head, update head pointer
- Return updated head
Full Transcript
To delete a node from a circular linked list, start at the head node. If the list is empty, stop immediately. Traverse the list checking each node's data against the key. Because the list is circular, if you return to the head without finding the key, the node is not present. When the node is found, check if it is the only node. If yes, set head to nil to empty the list. Otherwise, adjust the previous node's next pointer to skip the current node. If the node to delete is the head, update the head pointer to the next node. Finally, return the updated head pointer. This process ensures the circular linked list remains connected and valid after deletion.

Practice

(1/5)
1. What happens when you delete the only node in a circular linked list?
easy
A. The list still contains the deleted node.
B. The list becomes empty (head points to nil).
C. The list forms a linear linked list.
D. The list throws an error automatically.

Solution

  1. Step 1: Understand single-node circular list

    A circular linked list with one node points to itself.
  2. Step 2: Delete the only node

    Removing this node means no nodes remain, so head becomes nil.
  3. Final Answer:

    The list becomes empty (head points to nil). -> Option B
  4. Quick Check:

    Single node deleted = empty list [OK]
Hint: Deleting last node empties the list, head = nil [OK]
Common Mistakes:
  • Assuming list still has nodes after deleting the only node
  • Not updating head to nil
  • Confusing circular with linear list after deletion
2. Which Go syntax correctly moves the head pointer when deleting the head node from a circular linked list?
easy
A. head.next = head
B. head = nil.next
C. head = head.next
D. head = head.prev

Solution

  1. Step 1: Identify head deletion logic

    Deleting head means moving head pointer to next node.
  2. Step 2: Check syntax correctness

    Assigning head = head.next correctly moves head to next node.
  3. Final Answer:

    head = head.next -> Option C
  4. Quick Check:

    Move head to next node = head = head.next [OK]
Hint: Move head pointer to next node to delete head [OK]
Common Mistakes:
  • Using nil.next causes runtime error
  • Assigning head.next = head does not delete node
  • Using head.prev is invalid for singly circular list
3. Given this circular linked list: 1 -> 2 -> 3 -> (back to 1), what is the list after deleting node with value 2?
medium
A. 1 -> 3 -> (back to 1)
B. 1 -> 2 -> 3 -> (back to 1)
C. 2 -> 3 -> (back to 2)
D. 3 -> 1 -> (back to 3)

Solution

  1. Step 1: Locate node with value 2

    Node 2 is between nodes 1 and 3 in the circular list.
  2. Step 2: Remove node 2 by linking 1 to 3

    Update node 1's next pointer to node 3, skipping node 2.
  3. Final Answer:

    1 -> 3 -> (back to 1) -> Option A
  4. Quick Check:

    Deleted 2, linked 1 to 3 [OK]
Hint: Link previous node to next of deleted node [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node
  • Breaking circular link
4. What is wrong with this Go code snippet for deleting a node from a circular linked list?
func deleteNode(head *Node, key int) *Node {
  if head == nil {
    return nil
  }
  curr := head
  for curr.next != head && curr.next.data != key {
    curr = curr.next
  }
  if curr.next.data == key {
    curr.next = curr.next.next
  }
  return head
}
medium
A. It returns nil always.
B. It causes infinite loop always.
C. It deletes all nodes with the key.
D. It does not handle deleting the head node correctly.

Solution

  1. Step 1: Analyze deletion logic

    The code searches for node before target and skips target node.
  2. Step 2: Check head deletion case

    If the node to delete is head, head pointer is not updated, causing incorrect list.
  3. Final Answer:

    It does not handle deleting the head node correctly. -> Option D
  4. Quick Check:

    Head deletion needs head pointer update [OK]
Hint: Update head if deleting head node [OK]
Common Mistakes:
  • Ignoring head node deletion
  • Not updating head pointer
  • Assuming key always exists
5. You have a circular linked list: 10 -> 20 -> 30 -> 40 -> (back to 10). You want to delete node with value 10 (head). Which steps correctly update the list?
hard
A. Find last node (40), set its next to head.next (20), then update head to 20.
B. Set head to nil directly without updating last node.
C. Delete node 10 by setting head.next to nil.
D. Swap data of head and next node, then delete next node.

Solution

  1. Step 1: Find last node pointing to head

    Traverse list to find node 40 which points to head (10).
  2. Step 2: Update last node's next pointer

    Set node 40's next to head.next (node 20) to maintain circularity.
  3. Step 3: Update head pointer

    Set head to head.next (node 20) to delete original head.
  4. Final Answer:

    Find last node (40), set its next to head.next (20), then update head to 20. -> Option A
  5. Quick Check:

    Update last node and head to delete head node [OK]
Hint: Update last node's next and head to delete head [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Setting head to nil breaks list
  • Deleting node by setting next to nil
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