Challenge - 5 Problems
Circular Linked List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func printList(head *Node) { if head == nil { fmt.Println("List is empty") return } curr := head for { fmt.Printf("%d -> ", curr.data) curr = curr.next if curr == head { break } } fmt.Println("null") } func deleteNode(head *Node, key int) *Node { if head == nil { return nil } curr := head prev := head // Find last node to help with deletion for prev.next != head { prev = prev.next } // If list has only one node if head.data == key && head.next == head { return nil } // If head needs to be deleted if head.data == key { prev.next = head.next head = head.next return head } // Delete non-head node curr = head.next prev = head for curr != head { if curr.data == key { prev.next = curr.next return head } prev = curr curr = curr.next } return head } func main() { // Create circular linked list: 1 -> 2 -> 3 -> 4 -> 1 n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n4 := &Node{data: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n1 head := n1 head = deleteNode(head, 3) printList(head) }
Attempts:
2 left
💡 Hint
Remember to update the links so the node with value 3 is removed and the list remains circular.
✗ Incorrect
The node with value 3 is removed. The list links 2 directly to 4, maintaining the circular structure. The printed output shows the list starting from head: 1 -> 2 -> 4 -> null.
❓ Predict Output
intermediateOutput after deleting the head node
What is the printed state of the circular linked list after deleting the node with value 1 (the head)?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func printList(head *Node) { if head == nil { fmt.Println("List is empty") return } curr := head for { fmt.Printf("%d -> ", curr.data) curr = curr.next if curr == head { break } } fmt.Println("null") } func deleteNode(head *Node, key int) *Node { if head == nil { return nil } curr := head prev := head for prev.next != head { prev = prev.next } if head.data == key && head.next == head { return nil } if head.data == key { prev.next = head.next head = head.next return head } curr = head.next prev = head for curr != head { if curr.data == key { prev.next = curr.next return head } prev = curr curr = curr.next } return head } func main() { n1 := &Node{data: 1} n2 := &Node{data: 2} n3 := &Node{data: 3} n4 := &Node{data: 4} n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n1 head := n1 head = deleteNode(head, 1) printList(head) }
Attempts:
2 left
💡 Hint
Deleting the head means the new head is the next node, and the last node points to it.
✗ Incorrect
After deleting the head node 1, the new head is node 2. The list is 2 -> 3 -> 4 -> 2 circularly. Printing from head shows 2 -> 3 -> 4 -> null.
🔧 Debug
advancedIdentify the error in this deletion code
What error will this code produce when trying to delete a node from a circular linked list?
DSA Go
func deleteNode(head *Node, key int) *Node { if head == nil { return nil } curr := head prev := head // Incorrect loop condition for prev.next != nil { prev = prev.next } if head.data == key && head.next == head { return nil } if head.data == key { prev.next = head.next head = head.next return head } curr = head.next prev = head for curr != head { if curr.data == key { prev.next = curr.next return head } prev = curr curr = curr.next } return head }
Attempts:
2 left
💡 Hint
Check the loop condition for finding the last node in a circular linked list.
✗ Incorrect
The loop condition 'prev.next != nil' never becomes false in a circular linked list because the last node points back to head, not nil. This causes an infinite loop.
❓ Predict Output
advancedOutput after deleting the only node in the list
What is the printed output after deleting the only node (value 10) in a circular linked list?
DSA Go
package main import "fmt" type Node struct { data int next *Node } func printList(head *Node) { if head == nil { fmt.Println("List is empty") return } curr := head for { fmt.Printf("%d -> ", curr.data) curr = curr.next if curr == head { break } } fmt.Println("null") } func deleteNode(head *Node, key int) *Node { if head == nil { return nil } curr := head prev := head for prev.next != head { prev = prev.next } if head.data == key && head.next == head { return nil } if head.data == key { prev.next = head.next head = head.next return head } curr = head.next prev = head for curr != head { if curr.data == key { prev.next = curr.next return head } prev = curr curr = curr.next } return head } func main() { n1 := &Node{data: 10} n1.next = n1 head := n1 head = deleteNode(head, 10) printList(head) }
Attempts:
2 left
💡 Hint
Deleting the only node should result in an empty list.
✗ Incorrect
Since the list has only one node which is deleted, the function returns nil. The print function detects nil and prints 'List is empty'.
🧠 Conceptual
expertWhy is it necessary to find the last node when deleting the head in a circular linked list?
In a circular linked list, when deleting the head node, why must we find the last node before updating the head?
Attempts:
2 left
💡 Hint
Think about what keeps the list circular after the head changes.
✗ Incorrect
The last node points to the head in a circular linked list. When the head is deleted, the last node's next pointer must be updated to the new head to keep the list circular.
