Bird
0
0
DSA Cprogramming

Traversal of Circular Linked List in DSA C

Choose your learning style9 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.