Bird
Raised Fist0
DSA Cprogramming~20 mins

Traversal of Circular Linked List in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Circular Linked List Traversal
What is the output of the following C code that traverses a circular linked list with 3 nodes containing values 10, 20, and 30?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));
    struct Node* third = malloc(sizeof(struct Node));

    head->data = 10;
    head->next = second;

    second->data = 20;
    second->next = third;

    third->data = 30;
    third->next = head; // circular link

    struct Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");

    free(head);
    free(second);
    free(third);
    return 0;
}
A10 -> 20 -> 30 -> 10 -> 20 -> 30 -> null
B10 -> 20 -> null
C10 -> 20 -> 30 -> null
DRuntime error due to infinite loop
Attempts:
2 left
💡 Hint
Look carefully at the loop condition and how the traversal stops when it reaches the head again.
🧠 Conceptual
intermediate
1:00remaining
Number of Nodes Traversed in Circular Linked List
If a circular linked list has 5 nodes, how many nodes will be visited during a traversal that starts at the head and stops when it reaches the head again?
A5 nodes
BInfinite nodes
C6 nodes
D4 nodes
Attempts:
2 left
💡 Hint
Think about how the traversal stops exactly when it reaches the head again after visiting all nodes once.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Circular Linked List Traversal
What error will occur when running this code snippet that attempts to traverse a circular linked list?
DSA C
struct Node* temp = head;
while (temp != NULL) {
    printf("%d -> ", temp->data);
    temp = temp->next;
}
printf("null\n");
ACompilation error due to missing semicolon
BSegmentation fault due to accessing NULL pointer
CPrints all nodes correctly and stops
DInfinite loop because temp never becomes NULL in circular list
Attempts:
2 left
💡 Hint
Think about what happens to temp in a circular linked list when checking for NULL.
❓ Predict Output
advanced
2:00remaining
Output After Modifying Circular Linked List
What is the output after inserting a new node with value 15 between nodes with values 10 and 20 in the circular linked list and then traversing it?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));
    struct Node* third = malloc(sizeof(struct Node));
    struct Node* newNode = malloc(sizeof(struct Node));

    head->data = 10;
    second->data = 20;
    third->data = 30;
    newNode->data = 15;

    head->next = newNode;
    newNode->next = second;
    second->next = third;
    third->next = head;

    struct Node* temp = head;
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("null\n");

    free(head);
    free(second);
    free(third);
    free(newNode);
    return 0;
}
A10 -> 15 -> 30 -> 20 -> null
B10 -> 15 -> 20 -> 30 -> null
C10 -> 20 -> 15 -> 30 -> null
DRuntime error due to incorrect linking
Attempts:
2 left
💡 Hint
Check the order of next pointers after insertion.
🧠 Conceptual
expert
1:30remaining
Detecting Circular Linked List Using Fast and Slow Pointers
Which statement correctly describes how the fast and slow pointer method detects a circular linked list?
AIf fast pointer meets slow pointer, the list is circular
BIf fast pointer reaches NULL, the list is circular
CIf slow pointer reaches NULL, the list is circular
DIf slow pointer moves twice as fast as fast pointer, the list is circular
Attempts:
2 left
💡 Hint
Think about when two pointers meet in a circular structure.

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