📖 Scenario: Imagine you have a circular linked list representing players sitting around a round table. Each player has a unique number. Sometimes a player leaves the game, and you need to remove that player from the circle without breaking the circle.
🎯 Goal: You will build a circular linked list with exact player numbers, then write code to delete a player with a specific number from the list, and finally print the updated circle of players.
📋 What You'll Learn
Create a circular linked list with nodes containing player numbers: 10, 20, 30, 40, 50
Create a variable called key with the value 30 representing the player to remove
Write a function deleteNode that removes the node with the value key from the circular linked list
Print the circular linked list after deletion, showing player numbers separated by ' -> ' and ending with ' -> (back to head)'
💡 Why This Matters
🌍 Real World
Circular linked lists are used in real-world applications like multiplayer games where players take turns in a circle, or in operating systems for task scheduling where processes are cycled through repeatedly.
💼 Career
Understanding circular linked lists and how to manipulate them is important for software engineers working on systems programming, game development, and any application requiring cyclic data structures.
Progress0 / 4 steps
1
Create the Circular Linked List
Create a circular linked list with nodes containing player numbers 10, 20, 30, 40, and 50 in that order. Use a struct called Node with fields data int and next *Node. Create a variable called head pointing to the first node.
DSA Go
Hint
Start by defining the Node struct. Then create each node with the given numbers. Link each node's next to the next node. Finally, link the last node's next back to head to make the list circular.
2
Set the Player Number to Delete
Create an integer variable called key and set it to 30. This represents the player number to delete from the circular linked list.
DSA Go
Hint
Simply create a variable key and assign it the value 30.
3
Write the Delete Function
Write a function called deleteNode that takes two arguments: head *Node and key int. This function should remove the node with data == key from the circular linked list and return the new head. Use this function inside main to delete the node with the value stored in key.
DSA Go
Hint
Think about three cases: deleting the head node, deleting a node in the middle, and deleting when the node is not found. Remember to keep the list circular.
4
Print the Circular Linked List After Deletion
Write code to print the circular linked list starting from head. Print each node's data followed by " -> ". After printing all nodes, print "(back to head)" to show the circular nature. Use a for loop and stop when you reach head again after the first iteration.
DSA Go
Hint
Start printing from head. Use a loop that stops when you reach head again. Print each node's data followed by ' -> '. After the loop, print '(back to head)'.
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
Step 1: Understand single-node circular list
A circular linked list with one node points to itself.
Step 2: Delete the only node
Removing this node means no nodes remain, so head becomes nil.
Final Answer:
The list becomes empty (head points to nil). -> Option B
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
Step 1: Identify head deletion logic
Deleting head means moving head pointer to next node.
Step 2: Check syntax correctness
Assigning head = head.next correctly moves head to next node.
Final Answer:
head = head.next -> Option C
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
Step 1: Locate node with value 2
Node 2 is between nodes 1 and 3 in the circular list.
Step 2: Remove node 2 by linking 1 to 3
Update node 1's next pointer to node 3, skipping node 2.
Final Answer:
1 -> 3 -> (back to 1) -> Option A
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
Step 1: Analyze deletion logic
The code searches for node before target and skips target node.
Step 2: Check head deletion case
If the node to delete is head, head pointer is not updated, causing incorrect list.
Final Answer:
It does not handle deleting the head node correctly. -> Option D
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
Step 1: Find last node pointing to head
Traverse list to find node 40 which points to head (10).
Step 2: Update last node's next pointer
Set node 40's next to head.next (node 20) to maintain circularity.
Step 3: Update head pointer
Set head to head.next (node 20) to delete original head.
Final Answer:
Find last node (40), set its next to head.next (20), then update head to 20. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.