Bird
Raised Fist0
DSA Cprogramming

Traversal of Circular Linked List in DSA C

Choose your learning style10 modes available
Mental Model
A circular linked list loops back to the start, so traversal must stop when we return to the first node to avoid infinite loops.
Analogy: Imagine walking around a circular track; you keep moving forward until you reach the starting point again, then you stop.
head -> 1 -> 2 -> 3 ->
↑                   ↓
←←←←←←←←←←←←←←←←←←←
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> (back to 1), traverse and print all nodes
Goal: Visit each node exactly once and print its value
Step 1: start at head node with value 1
head -> [curr->1] -> 2 -> 3 -> (back to 1)
Why: We begin traversal from the first node
Step 2: print 1 and move curr to next node
head -> 1 -> [curr->2] -> 3 -> (back to 1)
Why: After visiting node 1, move to node 2
Step 3: print 2 and move curr to next node
head -> 1 -> 2 -> [curr->3] -> (back to 1)
Why: After visiting node 2, move to node 3
Step 4: print 3 and move curr to next node
head -> [curr->1] -> 2 -> 3 -> (back to 1)
Why: After visiting node 3, curr points back to head, so stop traversal
Result:
1 -> 2 -> 3 -> (back to 1) traversal complete
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for circular linked list
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to traverse and print circular linked list
void traverseCircularList(Node* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }
    Node* curr = head;
    do {
        printf("%d -> ", curr->data); // print current node
        curr = curr->next;           // move to next node
    } while (curr != head);          // stop when back to head
    printf("(back to %d)\n", head->data);
}

int main() {
    // Create nodes
    Node* head = createNode(1);
    Node* second = createNode(2);
    Node* third = createNode(3);

    // Link nodes in circular fashion
    head->next = second;
    second->next = third;
    third->next = head;

    // Traverse and print list
    traverseCircularList(head);

    // Free memory
    free(third);
    free(second);
    free(head);

    return 0;
}
if (head == NULL) {
handle empty list to avoid errors
Node* curr = head;
start traversal at head node
do {
begin loop to visit nodes at least once
printf("%d -> ", curr->data);
print current node's data
curr = curr->next;
advance curr to next node
} while (curr != head);
stop traversal when curr returns 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 based on returning to head instead of NULL, but time and space costs remain similar
Edge Cases
empty list (head is NULL)
prints 'List is empty' and stops without error
DSA C
if (head == NULL) {
single node circular list (node points to itself)
prints the single node once and stops correctly
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 stopping when you return to the start to avoid infinite loops.
Common Mistakes
Mistake: Using a while loop with condition curr != NULL, which never becomes false in circular lists
Fix: Use a do-while loop and stop when curr returns to head
Mistake: Not handling empty list (head == NULL) causing runtime errors
Fix: Add a check for head == NULL before traversal
Summary
It visits each node in a circular linked list exactly once by stopping when traversal returns to the start.
Use it when you need to process or print all elements in a circular linked list without infinite looping.
The key insight is to stop traversal when you reach the head node again, not when you find 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 at the last node with NULL next pointer.
B. In a circular linked list, traversal stops when the starting node is reached again.
C. In a circular linked list, traversal requires a double pointer.
D. In a circular linked list, traversal is done only once regardless of nodes.

Solution

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

    In a singly linked list, traversal stops when the next pointer is NULL, indicating the end.
  2. Step 2: Understand traversal stopping condition for circular linked list

    In a circular linked list, there is no NULL next pointer; traversal stops when the starting node is reached again.
  3. Final Answer:

    In a circular linked list, traversal stops when the starting node is reached again. -> Option B
  4. Quick Check:

    Traversal stops at start node in circular list [OK]
Hint: Stop traversal when you reach the start node again [OK]
Common Mistakes:
  • Assuming NULL marks end in circular list
  • Stopping traversal too early
  • Confusing circular with doubly linked list
  • Not checking for start node to stop
2. Which of the following is the correct syntax to start traversal from the head node in a circular linked list in C?
easy
A. Node* temp = head; for(int i=0; inext; }
B. Node* temp = head; while(temp != NULL) { /* process temp */ temp = temp->next; }
C. Node* temp = head; do { /* process temp */ temp = temp->next; } while(temp != head);
D. Node* temp = head; while(temp->next != NULL) { /* process temp */ temp = temp->next; }

Solution

  1. Step 1: Identify correct loop structure for circular linked list traversal

    Traversal must start at head and continue until we reach head again, so do-while loop is suitable.
  2. Step 2: Check each option's loop condition

    Node* temp = head; do { /* process temp */ temp = temp->next; } while(temp != head); uses do-while with condition temp != head, which correctly traverses all nodes once.
  3. Final Answer:

    Node* temp = head; do { /* process temp */ temp = temp->next; } while(temp != head); -> Option C
  4. Quick Check:

    Use do-while with temp != head for circular list [OK]
Hint: Use do-while loop to traverse circular linked list [OK]
Common Mistakes:
  • Using while(temp != NULL) in circular list
  • Using for loop without length variable
  • Checking temp->next != NULL which never happens
  • Starting traversal without processing head
3. What will be the output of the following C code snippet for a circular linked list with nodes containing values 10, 20, 30?
Node* temp = head;
do {
    printf("%d ", temp->data);
    temp = temp->next;
} while(temp != head);
medium
A. 10 20 30
B. 10 20 30 10
C. 20 30 10
D. 10 20

Solution

  1. Step 1: Understand traversal order in circular linked list

    Traversal starts at head (10), prints data, moves to next node until it reaches head again.
  2. Step 2: Trace the loop execution

    Prints 10, moves to 20, prints 20, moves to 30, prints 30, moves back to 10, loop ends.
  3. Final Answer:

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

    Print nodes once until back to head [OK]
Hint: Print nodes until you reach start node again [OK]
Common Mistakes:
  • Printing extra 10 at end
  • Starting from second node
  • Stopping before last node
  • Confusing circular with linear list output
4. Identify the error in the following code snippet for traversing a circular linked list:
Node* temp = head;
while(temp != NULL) {
    printf("%d ", temp->data);
    temp = temp->next;
    if(temp == head) break;
}
medium
A. The loop condition while(temp != NULL) is wrong for circular lists.
B. The break condition is incorrect; it should check temp->next.
C. The loop condition should be do-while, not while.
D. The printf statement should be after updating temp.

Solution

  1. Step 1: Analyze loop condition for circular linked list

    In circular linked list, next pointer is never NULL, so while(temp != NULL) will cause infinite loop.
  2. Step 2: Check break condition usage

    Break condition temp == head is correct to stop traversal after full cycle.
  3. Final Answer:

    The loop condition while(temp != NULL) is wrong for circular lists. -> Option A
  4. Quick Check:

    Use do-while or check for start node, not NULL [OK]
Hint: Never check NULL in circular list traversal [OK]
Common Mistakes:
  • Using while(temp != NULL) causing infinite loop
  • Incorrect break condition
  • Printing after moving temp
  • Confusing linear and circular list traversal
5. You have a circular linked list with nodes containing values 5, 15, 25, 35. You want to print all nodes except the node with value 25 during traversal. Which modification to the traversal code below achieves this?
Node* temp = head;
do {
    // print temp->data
    temp = temp->next;
} while(temp != head);
hard
A. Start traversal from node after 25.
B. Skip the node by setting temp->next = temp->next->next when temp->data == 25.
C. Break the loop when temp->data == 25.
D. Add an if condition: if(temp->data != 25) printf("%d ", temp->data);

Solution

  1. Step 1: Understand requirement to skip printing node with value 25

    We want to traverse all nodes but not print the node with data 25.
  2. Step 2: Modify printing logic inside traversal loop

    Add an if condition to print only if temp->data is not 25, so node is visited but not printed.
  3. Final Answer:

    Add an if condition: if(temp->data != 25) printf("%d ", temp->data); -> Option D
  4. Quick Check:

    Use if condition to skip printing specific node [OK]
Hint: Use if condition inside loop to skip unwanted node [OK]
Common Mistakes:
  • Modifying links during traversal causing list corruption
  • Breaking loop early missing nodes
  • Starting traversal from wrong node
  • Printing all nodes without condition
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