Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked 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 printed output of the following JavaScript code that traverses a circular linked list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

// Create nodes
const node1 = new Node(10);
const node2 = new Node(20);
const node3 = new Node(30);

// Link nodes in a circle
node1.next = node2;
node2.next = node3;
node3.next = node1;

// Traverse circular linked list starting from node1
let current = node1;
do {
  console.log(current.value);
  current = current.next;
} while (current !== node1);
A10\n20\n30
B10\n20\n30\n10
C10\n20
D20\n30\n10
Attempts:
2 left
💡 Hint
Remember that traversal stops when we reach the starting node again.
🧠 Conceptual
intermediate
1:30remaining
Understanding Circular Linked List Traversal Condition
In a circular linked list traversal, why do we use a do-while loop with the condition `current !== head` instead of a while loop with `current !== null`?
ABecause circular linked lists have no null end, so we stop when we reach the starting node again.
BBecause the head node is always null in circular linked lists.
CBecause the traversal must run exactly twice to cover all nodes.
DBecause the next pointer is always null in circular linked lists.
Attempts:
2 left
💡 Hint
Think about what marks the end of traversal in a circular linked list.
❓ Predict Output
advanced
2:00remaining
Output After Inserting Node in Circular Linked List
What is the printed output after inserting a new node with value 25 between nodes with values 20 and 30 in the circular linked list below?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const node1 = new Node(10);
const node2 = new Node(20);
const node3 = new Node(30);

node1.next = node2;
node2.next = node3;
node3.next = node1;

// Insert new node with value 25 between node2 and node3
const node4 = new Node(25);
node2.next = node4;
node4.next = node3;

// Traverse and print
let current = node1;
do {
  console.log(current.value);
  current = current.next;
} while (current !== node1);
A10\n25\n20\n30
B10\n20\n30\n25
C10\n20\n25\n30
D10\n20\n30
Attempts:
2 left
💡 Hint
Check the links after insertion carefully.
🔧 Debug
advanced
1:30remaining
Identify the Bug in Circular Linked List Traversal
What error will occur when running the following code to traverse a circular linked list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

const node1 = new Node(1);
const node2 = new Node(2);
const node3 = new Node(3);

node1.next = node2;
node2.next = node3;
node3.next = node1;

let current = node1;
while (current !== node1) {
  console.log(current.value);
  current = current.next;
}
ATypeError because current is null.
BInfinite loop printing 1, 2, 3 repeatedly.
CSyntaxError due to missing semicolon.
DThe loop never runs and prints nothing.
Attempts:
2 left
💡 Hint
Check the loop condition and initial value of current.
🚀 Application
expert
2:30remaining
Count Nodes in Circular Linked List
Given a circular linked list with unknown length, which code snippet correctly counts the number of nodes?
A
let count = 0;
let current = head;
while (current !== null) {
  count++;
  current = current.next;
}
console.log(count);
B
let count = 0;
let current = head;
do {
  count++;
  current = current.next;
} while (current !== head);
console.log(count);
C
let count = 1;
let current = head.next;
while (current !== head) {
  count++;
  current = current.next;
}
console.log(count);
D
let count = 0;
let current = head.next;
do {
  count++;
  current = current.next;
} while (current !== head);
console.log(count);
Attempts:
2 left
💡 Hint
Remember to count the head node and stop after full circle.

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 requires a double pointer.
B. In a circular linked list, traversal stops when the starting node is reached again.
C. In a circular linked list, traversal stops when the last node's next is null.
D. In a circular linked list, traversal is done only backwards.

Solution

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

    Traversal stops when the next pointer is null, indicating the end.
  2. Step 2: Understand traversal stopping condition in 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 B
  4. Quick Check:

    Stopping condition differs: null vs start node [OK]
Hint: Remember: circular list loops back to start node [OK]
Common Mistakes:
  • Stopping traversal at null in circular list
  • Assuming circular list traversal is backward only
  • Confusing traversal conditions with doubly linked lists
2. Which of the following is the correct syntax to start traversing a circular linked list in JavaScript assuming head is the starting node?
easy
A. let current = head; do { /* process current */ current = current.next; } while(current !== head);
B. let current = head; while(current.next !== null) { /* process current */ current = current.next; }
C. for(let current = head; current !== null; current = current.next) { /* process current */ }
D. let current = head; while(current !== null) { /* process current */ current = current.next; }

Solution

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

    Traversal must continue until current node loops back to head, so a do-while loop is suitable.
  2. Step 2: Check syntax correctness

    let current = head; do { /* process current */ current = current.next; } while(current !== head); uses do-while with condition current !== head, correctly handling circular traversal.
  3. Final Answer:

    let current = head; do { /* process current */ current = current.next; } while(current !== head); -> Option A
  4. Quick Check:

    Use do-while to handle circular traversal [OK]
Hint: Use do-while to ensure at least one iteration [OK]
Common Mistakes:
  • Using while with null condition in circular list
  • Forgetting to process the head node
  • Using for loop with null condition in circular list
3. What will be the output of the following code snippet?
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

const head = new Node(1);
const second = new Node(2);
const third = new Node(3);

head.next = second;
second.next = third;
third.next = head; // circular link

let current = head;
do {
  console.log(current.data);
  current = current.next;
} while(current !== head);
medium
A. 1 2 3 1
B. Error: Infinite loop
C. 1 2 3 1 2 3 ... (infinite loop)
D. 1 2 3

Solution

  1. Step 1: Understand the circular linked list structure

    Nodes 1, 2, 3 are linked circularly: third.next points back to head.
  2. Step 2: Trace the do-while loop output

    Loop prints current.data and moves current to next until current equals head again. It prints 1, 2, 3 then stops.
  3. Final Answer:

    1 2 3 -> Option D
  4. Quick Check:

    Loop prints nodes once until back to start [OK]
Hint: do-while prints all nodes once in circular list [OK]
Common Mistakes:
  • Expecting infinite loop output
  • Printing head node twice
  • Confusing do-while with while loop behavior
4. Identify the error in the following circular linked list traversal code:
let current = head;
while(current !== head) {
  console.log(current.data);
  current = current.next;
}
medium
A. The loop skips the last node.
B. The loop causes an infinite loop.
C. The loop never executes because current equals head initially.
D. The code throws a runtime error due to null reference.

Solution

  1. Step 1: Analyze initial condition of the while loop

    current is initialized to head, so current !== head is false at start.
  2. Step 2: Understand loop execution

    Since condition is false initially, loop body never executes, so no traversal happens.
  3. Final Answer:

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

    While condition false at start means zero iterations [OK]
Hint: Check initial loop condition carefully in circular traversal [OK]
Common Mistakes:
  • Using while instead of do-while for circular lists
  • Assuming loop runs at least once
  • Ignoring initial condition impact
5. Given a circular linked list where each node contains an integer, how would you find the sum of all node values without getting stuck in an infinite loop?
hard
A. Use a do-while loop starting at head, add node values, stop when current equals head again.
B. Use a while loop checking for current.next !== null to sum values.
C. Use recursion without a base case to sum values.
D. Traverse nodes until current is null, summing values.

Solution

  1. Step 1: Choose traversal method suitable for circular list

    Use do-while loop to ensure all nodes are visited once, stopping when current returns to head.
  2. Step 2: Sum node values during traversal

    Initialize sum to 0, add current.data each iteration, move to next node until loop ends.
  3. Final Answer:

    Use a do-while loop starting at head, add node values, stop when current equals head again. -> Option A
  4. Quick Check:

    Do-while loop prevents infinite traversal in circular list [OK]
Hint: Stop traversal when current node equals start node [OK]
Common Mistakes:
  • Using null check in circular list traversal
  • Forgetting to stop at head node again
  • Using recursion without base case causing infinite calls
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