Bird
Raised Fist0
DSA C#programming

Traversal of Circular Linked List in DSA C#

Choose your learning style10 modes available
Mental Model
A circular linked list connects the last node back to the first, so traversal loops until we return to the start.
Analogy: Imagine a merry-go-round where you keep moving from one horse to the next until you come back to the horse you started on.
1 -> 2 -> 3 ->
↑           ↓
← ← ← ← ← ←
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular, last node points back to 1), traverse all nodes
Goal: Visit each node exactly once and stop after returning to the start
Step 1: Start at head node with value 1
[curr->1] -> 2 -> 3 -> back to 1
Why: We begin traversal from the first node
Step 2: Move curr to next node with value 2
1 -> [curr->2] -> 3 -> back to 1
Why: Visit the next node in the list
Step 3: Move curr to next node with value 3
1 -> 2 -> [curr->3] -> back to 1
Why: Continue traversal to next node
Step 4: Move curr to next node which is head (1), stop traversal
[curr->1] -> 2 -> 3 -> back to 1
Why: We reached the start again, so traversal ends
Result:
1 -> 2 -> 3 -> back to 1 (traversal complete)
Annotated Code
DSA C#
using System;

public class Node
{
    public int data;
    public Node next;

    public Node(int data)
    {
        this.data = data;
        this.next = null;
    }
}

public class CircularLinkedList
{
    public Node head;

    public void Traverse()
    {
        if (head == null)
        {
            Console.WriteLine("List is empty");
            return;
        }

        Node curr = head;
        do
        {
            Console.Write(curr.data + " -> ");
            curr = curr.next; // advance curr to next node
        } while (curr != head); // stop when we reach head again

        Console.WriteLine("back to " + head.data);
    }
}

public class Program
{
    public static void Main()
    {
        CircularLinkedList cll = new CircularLinkedList();
        cll.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);

        cll.head.next = second;
        second.next = third;
        third.next = cll.head; // make it circular

        cll.Traverse();
    }
}
Node curr = head;
start traversal at head node
do { Console.Write(curr.data + " -> "); curr = curr.next; } while (curr != head);
visit current node and move to next until we return to head
OutputSuccess
1 -> 2 -> 3 -> back to 1
Complexity Analysis
Time: O(n) because we visit each of the n nodes exactly once during traversal
Space: O(1) because we use only a few pointers regardless of list size
vs Alternative: Compared to linear linked list traversal, circular traversal requires a stopping condition to detect when we loop back to start instead of null
Edge Cases
empty list (head is null)
Traversal prints 'List is empty' and stops
DSA C#
if (head == null) { Console.WriteLine("List is empty"); return; }
single node circular list (node points to itself)
Traversal prints the single node once and stops after returning to head
DSA C#
do { ... } while (curr != head);
When to Use This Pattern
When you see a linked list where the last node points back to the first, use circular linked list traversal by looping until you return to the start node.
Common Mistakes
Mistake: Using a null check to stop traversal instead of checking if current node is head again
Fix: Change stopping condition to 'while (curr != head)' to handle circular structure
Mistake: Not handling empty list case causing null reference errors
Fix: Add a check for head == null before traversal
Summary
It visits each node in a circular linked list exactly once by looping until it returns to the start.
Use it when you need to process all nodes in a circular linked list without infinite looping.
The key is to stop traversal when you reach the starting node again, not when you hit null.

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

  1. Step 1: Understand traversal stopping condition for singly linked list

    Traversal stops when the next pointer is null, indicating the end of the list.
  2. 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.
  3. Final Answer:

    In a circular linked list, traversal stops when the starting node is reached again. -> Option D
  4. 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

  1. 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.
  2. Step 2: Check condition to stop traversal

    Traversal stops when current node returns to head, so condition is current != head in while.
  3. Final Answer:

    Node current = head; do { Console.WriteLine(current.Data); current = current.Next; } while (current != head); -> Option C
  4. 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

  1. Step 1: Understand the circular linked list structure

    The list nodes are 10 -> 20 -> 30, and 30 points back to 10, making it circular.
  2. 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.
  3. Final Answer:

    10 -> 20 -> 30 -> null -> Option A
  4. 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

  1. Step 1: Analyze the while loop condition

    The condition current != null is incorrect for circular linked list because next is never null.
  2. Step 2: Identify correct stopping condition

    The loop should stop when current returns to head, so condition should be current != head.
  3. Final Answer:

    The while condition should check current != head instead of current != null. -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Start at head, use do-while loop adding node data, stop when current returns to head. -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard