Bird
Raised Fist0
DSA Javaprogramming

Traversal of Circular Linked List in DSA Java

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 people sitting in a circle passing a ball; you keep passing until the ball comes back to the first person.
1 -> 2 -> 3 ->
↑           ↓
←←←←←←←←←←←
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 (circular), traverse and print all nodes
Goal: Visit each node exactly once and print its value
Step 1: start at head node with value 1
1 [curr↑] -> 2 -> 3 -> back to 1
Why: We begin traversal from the first node
Step 2: print current node value 1 and move curr to next node
1 -> 2 [curr↑] -> 3 -> back to 1
Why: Print current and move forward to visit next node
Step 3: print current node value 2 and move curr to next node
1 -> 2 -> 3 [curr↑] -> back to 1
Why: Continue visiting nodes in order
Step 4: print current node value 3 and move curr to next node
1 -> 2 -> 3 -> 1 [curr↑]
Why: After last node, curr points back to head
Step 5: stop traversal because curr is back at head
1 -> 2 -> 3 -> back to 1 (stop)
Why: We have visited all nodes once; stop to avoid infinite loop
Result:
1 -> 2 -> 3 -> back to 1 (traversal complete)
Annotated Code
DSA Java
class Node {
    int data;
    Node next;
    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class CircularLinkedListTraversal {
    Node head;

    // Method to traverse and print circular linked list
    void traverse() {
        if (head == null) return; // empty list check
        Node curr = head;
        do {
            System.out.print(curr.data + " -> "); // print current node
            curr = curr.next; // move to next node
        } while (curr != head); // stop when back to head
        System.out.print("head\n"); // end of traversal
    }

    public static void main(String[] args) {
        CircularLinkedListTraversal list = new CircularLinkedListTraversal();
        // create nodes
        list.head = new Node(1);
        Node second = new Node(2);
        Node third = new Node(3);
        // link nodes in circular way
        list.head.next = second;
        second.next = third;
        third.next = list.head;

        list.traverse();
    }
}
if (head == null) return; // empty list check
stop if list is empty to avoid errors
Node curr = head;
start traversal from head node
do {
begin loop to visit nodes at least once
System.out.print(curr.data + " -> ");
print current node's data
curr = curr.next;
advance curr to next node
} while (curr != head);
continue until we return to head node
System.out.print("head\n");
print head to mark end of traversal
OutputSuccess
1 -> 2 -> 3 -> head
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 avoid infinite loops, but time complexity remains O(n)
Edge Cases
empty list (head is null)
traversal immediately returns without printing
DSA Java
if (head == null) return; // empty list check
single node circular list (node points to itself)
prints the single node once and stops
DSA Java
do { ... } while (curr != head); // stops after one full loop
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 with a do-while loop to visit all nodes exactly once without infinite looping.
Common Mistakes
Mistake: Using a while loop with condition curr != null, which never ends in circular lists
Fix: Use a do-while loop with condition curr != head to stop after one full cycle
Mistake: Not checking for empty list before traversal causing null pointer errors
Fix: Add a check if head is null before starting 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 or print all elements in a circular linked list safely.
The key insight is to stop traversal when you reach the starting node again to avoid infinite loops.

Practice

(1/5)
1. What is the main difference between traversing a circular linked list and a singly linked list?
easy
A. In a singly linked list, traversal stops when the starting node is reached again.
B. In a circular linked list, traversal stops when the last node's next is null.
C. Traversal is the same for both lists.
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:

    Stopping condition differs: null vs start node [OK]
Hint: Remember circular lists loop back to start node [OK]
Common Mistakes:
  • Stopping traversal at null in circular list
  • Confusing stopping conditions between list types
  • Assuming circular list ends with null pointer
2. Which of the following Java code snippets correctly starts traversal of a circular linked list from the head node?
easy
A. Node current = head; do { System.out.println(current.data); current = current.next; } while(current != head);
B. Node current = head; while(current != null) { System.out.println(current.data); current = current.next; }
C. Node current = head.next; while(current != head) { System.out.println(current.data); current = current.next; }
D. Node current = null; do { System.out.println(current.data); current = current.next; } while(current != head);

Solution

  1. Step 1: Check initialization and loop condition

    Node current = head; do { System.out.println(current.data); current = current.next; } while(current != head); initializes current to head and uses a do-while loop to print and move until current equals head again, which is correct for circular lists.
  2. Step 2: Analyze other options

    Node current = head; while(current != null) { System.out.println(current.data); current = current.next; } uses while(current != null), which is incorrect for circular lists as next is never null. Node current = head.next; while(current != head) { System.out.println(current.data); current = current.next; } starts from head.next and misses printing head data. Node current = null; do { System.out.println(current.data); current = current.next; } while(current != head); initializes current to null, causing NullPointerException.
  3. Final Answer:

    Node current = head; do { System.out.println(current.data); current = current.next; } while(current != head); -> Option A
  4. Quick Check:

    Use do-while with current != head [OK]
Hint: Use do-while loop starting at head for circular lists [OK]
Common Mistakes:
  • Using while(current != null) for circular lists
  • Starting traversal from head.next and missing head node
  • Initializing current to null causing errors
3. What will be the output of the following 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 {
    System.out.print(current.data + " ");
    current = current.next;
} while(current != head);
medium
A. 10 20 30 10
B. 10 20 30
C. 20 30 10
D. 10 20 30 30

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 loop output

    Starting at head (10), print 10, move to 20, print 20, move to 30, print 30, move to 10 again. Loop stops when current == head, so 10 is not printed again.
  3. Final Answer:

    10 20 30 -> Option B
  4. Quick Check:

    Print nodes once until back to start [OK]
Hint: Do-while prints all nodes once before stopping [OK]
Common Mistakes:
  • Printing the start node twice
  • Missing the first node in output
  • Printing extra nodes beyond the loop
4. Identify the error in the following code snippet for traversing a circular linked list:
Node current = head;
while(current != head) {
    System.out.print(current.data + " ");
    current = current.next;
}
medium
A. The loop never executes because current == head initially.
B. The loop causes infinite printing of nodes.
C. The code throws NullPointerException.
D. The loop skips the last node.

Solution

  1. Step 1: Analyze loop condition with initial current value

    current is initialized to head, so current != head is false at start, loop body never runs.
  2. Step 2: Understand correct loop for circular list traversal

    Use do-while loop to ensure at least one iteration, since current starts at head.
  3. Final Answer:

    The loop never executes because current == head initially. -> Option A
  4. Quick Check:

    while(current != head) false at start [OK]
Hint: Use do-while to run loop at least once [OK]
Common Mistakes:
  • Using while instead of do-while for circular lists
  • Not printing the first node
  • Assuming loop runs when condition false initially
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 below correctly handles the traversal without infinite loop?
hard
A. Initialize sum=0; Node current=head.next; while(current != head) { sum += current.data; current = current.next; } return sum;
B. Initialize sum=0; Node current=head; while(current != null) { sum += current.data; current = current.next; } return sum;
C. Initialize sum=0; Node current=head; do { sum += current.data; current = current.next; } while(current != head); return sum;
D. Initialize sum=0; Node current=null; do { sum += current.data; current = current.next; } while(current != head); return sum;

Solution

  1. Step 1: Understand traversal stopping condition for circular list

    Traversal must stop when current returns to head to avoid infinite loop.
  2. Step 2: Analyze each option's traversal logic

    Initialize sum=0; Node current=head; do { sum += current.data; current = current.next; } while(current != head); return sum; uses do-while starting at head, summing all nodes once. Initialize sum=0; Node current=head; while(current != null) { sum += current.data; current = current.next; } return sum; uses while(current != null), which never happens in circular list. Initialize sum=0; Node current=head.next; while(current != head) { sum += current.data; current = current.next; } return sum; starts at head.next, missing head's data. Initialize sum=0; Node current=null; do { sum += current.data; current = current.next; } while(current != head); return sum; initializes current to null causing error.
  3. Final Answer:

    Initialize sum=0; Node current=head; do { sum += current.data; current = current.next; } while(current != head); return sum; -> Option C
  4. Quick Check:

    Use do-while with current != head for full traversal [OK]
Hint: Sum with do-while loop until current equals head [OK]
Common Mistakes:
  • Using while(current != null) causing infinite loop
  • Skipping head node in sum calculation
  • Initializing current to null causing errors
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