📖 Scenario: Imagine you have a group of friends sitting around a round table. You want to greet each friend one by one, starting from a specific person and going around the table until you come back to the same person.
🎯 Goal: You will create a circular linked list representing friends sitting around the table. Then, you will write code to visit each friend exactly once and print their names in order.
📋 What You'll Learn
Create a circular linked list with exactly three nodes named head, second, and third.
Each node must have a data field with the friend's name and a next pointer.
The next pointer of the last node (third) must point back to head to form a circle.
Use a current variable to traverse the circular linked list starting from head.
Print each friend's name exactly once during traversal.
💡 Why This Matters
🌍 Real World
Circular linked lists are used in real life to model situations where the end connects back to the beginning, like players taking turns in a game or a playlist that repeats songs.
💼 Career
Understanding circular linked lists helps in jobs involving system design, game development, and any software that requires cyclic data processing or scheduling.
Progress0 / 4 steps
1
Create Circular Linked List Nodes
Create a class called Node with a public string field data and a public Node field next. Then create three Node objects named head, second, and third. Assign head.data = "Alice", second.data = "Bob", and third.data = "Charlie".
DSA C#
Hint
Start by defining the Node class with two public fields. Then create three instances and assign their data values.
2
Link Nodes to Form Circular List
Set head.next to second, second.next to third, and third.next to head to complete the circle.
DSA C#
Hint
Connect each node's next to the following node. The last node should point back to head.
3
Traverse the Circular Linked List
Create a Node variable called current and set it to head. Use a do-while loop to print current.data and then move current to current.next. Continue looping until current is head again.
DSA C#
Hint
Use a do-while loop to ensure you visit each node once, starting from head and stopping when you return to head.
4
Print the Circular Linked List Traversal
Run the program to print the names of the friends in order: Alice, Bob, and Charlie, each on a new line.
DSA C#
Hint
Run the program and check that the output lists all three names in order, each on its own line.
Practice
(1/5)
1. What is the main difference between traversing a circular linked list and a singly linked list?
easy
A. In a circular linked list, traversal stops when the last node's next is null.
B. In a singly linked list, traversal stops when the first node is reached again.
C. In a singly linked list, traversal never ends.
D. In a circular linked list, traversal stops when the starting node is reached again.
Solution
Step 1: Understand traversal stopping condition for singly linked list
Traversal stops when the next pointer is null, indicating the end of the list.
Step 2: Understand traversal stopping condition for circular linked list
Traversal stops when the traversal reaches the starting node again, since the list loops back.
Final Answer:
In a circular linked list, traversal stops when the starting node is reached again. -> Option D
Quick Check:
Circular list stops at start node [OK]
Hint: Remember: circular list loops back to start node [OK]
Common Mistakes:
Thinking traversal stops at null in circular list
Confusing stopping conditions of singly and circular lists
Assuming circular list has a null next pointer
2. Which of the following C# code snippets correctly starts traversal of a circular linked list from the head node?
easy
A. Node current = head; for (int i = 0; i < length; i++) { Console.WriteLine(current.Data); current = current.Next; }
B. Node current = head; while (current != null) { Console.WriteLine(current.Data); current = current.Next; }
C. Node current = head; do { Console.WriteLine(current.Data); current = current.Next; } while (current != head);
D. Node current = head; while (current.Next != null) { Console.WriteLine(current.Data); current = current.Next; }
Solution
Step 1: Identify correct loop structure for circular list traversal
Traversal must use a do-while loop to ensure the head node is processed before checking the condition.
Step 2: Check condition to stop traversal
Traversal stops when current node returns to head, so condition is current != head in while.
Final Answer:
Node current = head; do { Console.WriteLine(current.Data); current = current.Next; } while (current != head); -> Option C
Quick Check:
Use do-while with current != head [OK]
Hint: Use do-while loop to handle circular traversal [OK]
Common Mistakes:
Using while loop without do causes skipping head node
Checking for null in circular list causes infinite loop
Using for loop without length in circular list
3. What will be the output of the following C# code snippet for a circular linked list with nodes containing data 10 -> 20 -> 30 -> (back to 10)?
Node head = new Node(10);
head.Next = new Node(20);
head.Next.Next = new Node(30);
head.Next.Next.Next = head;
Node current = head;
do {
Console.Write(current.Data + " -> ");
current = current.Next;
} while (current != head);
Console.WriteLine("null");
medium
A. 10 -> 20 -> 30 -> null
B. 10 -> 20 -> 30 -> 10 -> null
C. 10 -> 20 -> 30 -> 20 -> null
D. 10 -> 20 -> 30 -> 30 -> null
Solution
Step 1: Understand the circular linked list structure
The list nodes are 10 -> 20 -> 30, and 30 points back to 10, making it circular.
Step 2: Trace the do-while traversal
Traversal starts at 10, prints 10, moves to 20, prints 20, moves to 30, prints 30, then moves back to 10 which stops the loop.
Final Answer:
10 -> 20 -> 30 -> null -> Option A
Quick Check:
Traversal prints nodes once until back to head [OK]
Hint: Traversal prints nodes once until back to head [OK]
Common Mistakes:
Printing extra nodes after loop ends
Confusing circular list with linear list output
Not stopping traversal at head node
4. Identify the error in the following C# code snippet that attempts to traverse a circular linked list:
Node current = head;
while (current != null) {
Console.WriteLine(current.Data);
current = current.Next;
if (current == head) break;
}
medium
A. The while condition should check current != head instead of current != null.
B. The while condition should be do-while to include head node.
C. The break condition is missing to stop infinite loop.
D. The code will cause a null reference exception.
Solution
Step 1: Analyze the while loop condition
The condition current != null is incorrect for circular linked list because next is never null.
Step 2: Identify correct stopping condition
The loop should stop when current returns to head, so condition should be current != head.
Final Answer:
The while condition should check current != head instead of current != null. -> Option A
Quick Check:
Use current != head to stop traversal [OK]
Hint: Check for current == head, not null, in circular list [OK]
Common Mistakes:
Using null check in circular list traversal
Missing break condition causing infinite loop
Using while instead of do-while causing skipped node
5. Given a circular linked list where each node contains an integer, write a method to find the sum of all node values. Which approach correctly handles the circular nature without infinite looping?
hard
A. Start at head, use while loop with condition current != null, add node data each iteration.
B. Start at head, use do-while loop adding node data, stop when current returns to head.
C. Start at head, use for loop with fixed length equal to number of nodes, add node data.
D. Start at head, use while loop with condition current.Next != null, add node data.
Solution
Step 1: Understand circular list traversal for summation
Traversal must visit each node exactly once and stop when current returns to head to avoid infinite loop.
Step 2: Choose correct loop structure
Using a do-while loop starting at head and stopping when current == head ensures all nodes are included once.
Final Answer:
Start at head, use do-while loop adding node data, stop when current returns to head. -> Option B
Quick Check:
Use do-while and stop at head for sum [OK]
Hint: Use do-while and stop when current == head [OK]
Common Mistakes:
Using null checks causing infinite loops
Missing last node in sum due to wrong loop
Using for loop without knowing list length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.