Imagine you have a list of players in a game that repeats turns endlessly. Why is a circular linked list better than a singly linked list for this?
Think about what happens when you reach the end of the list and want to continue.
A circular linked list connects the last node back to the first, allowing infinite looping without extra code to restart traversal.
What will be the printed output after running this Go code that traverses a circular linked list with 3 nodes?
package main import "fmt" type Node struct { value int next *Node } func main() { n1 := &Node{value: 1} n2 := &Node{value: 2} n3 := &Node{value: 3} n1.next = n2 n2.next = n3 n3.next = n1 // circular link current := n1 count := 0 for count < 5 { fmt.Print(current.value, " -> ") current = current.next count++ } fmt.Print("null") }
Count how many nodes are printed and remember the circular link.
The loop runs 5 times, printing nodes in order: 1, 2, 3, then back to 1, then 2, then stops.
What error will this Go code cause when inserting a new node in a circular linked list?
package main import "fmt" type Node struct { value int next *Node } func insertAfter(node *Node, val int) { newNode := &Node{value: val} newNode.next = node.next node.next = newNode } func main() { n1 := &Node{value: 1} n1.next = n1 // circular link single node insertAfter(n1, 2) insertAfter(n1.next, 3) current := n1 for i := 0; i < 4; i++ { fmt.Print(current.value, " -> ") current = current.next } fmt.Print("null") }
Check how the insertAfter function updates links and the loop count in main.
The insertAfter function correctly inserts nodes after the given node. The loop prints 4 nodes, so output is 1, 2, 3, then back to 1.
Which real-world scenario is best suited for implementing with a circular linked list?
Think about continuous cycling without stopping.
A circular linked list naturally loops back to the start, perfect for repeating playlists without extra code.
What is the output after deleting a node with value 2 from this circular linked list?
package main import "fmt" type Node struct { value int next *Node } func deleteNode(head *Node, val int) *Node { if head == nil { return nil } current := head var prev *Node for { if current.value == val { if prev != nil { prev.next = current.next } else { // Deleting head node temp := head for temp.next != head { temp = temp.next } if head.next == head { return nil } temp.next = head.next head = head.next } break } prev = current current = current.next if current == head { break } } return head } func printList(head *Node, count int) { current := head for i := 0; i < count; i++ { fmt.Print(current.value, " -> ") current = current.next } fmt.Print("null") } func main() { n1 := &Node{value: 1} n2 := &Node{value: 2} n3 := &Node{value: 3} n1.next = n2 n2.next = n3 n3.next = n1 head := n1 head = deleteNode(head, 2) printList(head, 4) }
Check how the deleteNode function updates links and what printList prints.
After deleting node with value 2, the list is 1 -> 3 -> back to 1. Printing 4 nodes cycles through 1, 3, 1, 3.
