📖 Scenario: Imagine you are working on a system that manages a playlist of songs. The playlist is represented as a linked list where each song points to the next song. Sometimes, due to a bug, the playlist might loop back to an earlier song, creating a circular playlist. Your task is to detect if the playlist is circular to avoid infinite loops when playing songs.
🎯 Goal: You will build a simple linked list and write code to check if the linked list is circular (loops back to an earlier node).
📋 What You'll Learn
Create a linked list with exactly 4 nodes with values 10, 20, 30, and 40
Create a variable called head that points to the first node
Create a variable called isCircular to store the result of the circular check
Use a while loop with two pointers slow and fast to detect circularity
Print "Circular" if the list is circular, otherwise print "Not Circular"
💡 Why This Matters
🌍 Real World
Detecting circular linked lists is important in systems like music playlists, task schedulers, and network routing to avoid infinite loops.
💼 Career
Understanding linked lists and cycle detection is a common interview question and a fundamental skill for software developers working with data structures.
Progress0 / 4 steps
1
Create the linked list nodes and connect them
Create four linked list nodes named node1, node2, node3, and node4 with values 10, 20, 30, and 40 respectively. Connect them so that node1.next is node2, node2.next is node3, and node3.next is node4. Set node4.next to null.
DSA Javascript
Hint
Think of each node as a box holding a number and a pointer to the next box. Connect the boxes in order.
2
Set the head of the linked list
Create a variable called head and set it to node1 to mark the start of the linked list.
DSA Javascript
Hint
The head is like the first song in the playlist. It tells us where to start.
3
Write the logic to detect if the linked list is circular
Create a variable called isCircular and set it to false. Use two pointers named slow and fast, both starting at head. Use a while loop that continues as long as fast and fast.next are not null. Inside the loop, move slow one step forward (slow = slow.next) and fast two steps forward (fast = fast.next.next). If at any point slow equals fast, set isCircular to true and break the loop.
DSA Javascript
Hint
Use two pointers moving at different speeds. If they meet, the list loops back.
4
Print if the linked list is circular or not
Write a console.log statement that prints "Circular" if isCircular is true, otherwise prints "Not Circular".
DSA Javascript
Hint
Use a simple console.log with a condition to print the result.
Practice
(1/5)
1. What does it mean if a linked list is circular?
easy
A. The linked list has only one node
B. The linked list has no nodes
C. The linked list nodes are sorted in ascending order
D. The last node points back to one of the previous nodes, forming a loop
Solution
Step 1: Understand linked list structure
A linked list is a chain of nodes where each node points to the next node.
Step 2: Define circular linked list
If the last node points back to any previous node, it creates a loop, making the list circular.
Final Answer:
The last node points back to one of the previous nodes, forming a loop -> Option D
Quick Check:
Circular linked list = last node points back [OK]
Hint: Circular means last node links back to earlier node [OK]
Common Mistakes:
Thinking circular means sorted list
Confusing empty list with circular list
Assuming single node list is always circular
2. Which of the following is the correct way to check if a linked list is circular in JavaScript?
easy
A. Count nodes and compare with array length
B. Use two pointers moving at different speeds and check if they meet
C. Check if head is null only
D. Sort the linked list nodes
Solution
Step 1: Understand Floyd's cycle detection
Use two pointers: slow moves one step, fast moves two steps. If they meet, list is circular.
Step 2: Eliminate wrong options
Checking head null only doesn't detect loops. Counting nodes or sorting is unrelated.
Final Answer:
Use two pointers moving at different speeds and check if they meet -> Option B
Quick Check:
Two-pointer cycle detection = correct method [OK]
Hint: Two pointers meeting means circular list [OK]
Common Mistakes:
Only checking if head is null
Trying to sort linked list to detect cycle
Counting nodes without loop detection
3. What will be the output of the following code snippet?
class Node {
constructor(value) {
this.value = value;
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 = second; // creates a loop
function isCircular(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}
console.log(isCircular(head));
medium
A. false
B. undefined
C. true
D. TypeError
Solution
Step 1: Analyze linked list structure
Nodes 1 -> 2 -> 3, and third node points back to second, creating a loop.
Step 2: Trace two-pointer loop detection
Slow and fast pointers will eventually meet inside the loop, so function returns true.
Final Answer:
true -> Option C
Quick Check:
Loop detected = true [OK]
Hint: Loop causes pointers to meet, returning true [OK]
Common Mistakes:
Assuming no loop because list looks linear
Confusing pointer equality check
Expecting false without tracing pointers
4. Find the error in this code that tries to detect a circular linked list:
function isCircular(head) {
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) return true;
}
return false;
}
medium
A. The while condition should check if fast and fast.next exist
B. The comparison should use === instead of ==
C. The slow pointer should move two steps instead of one
D. The function should return false inside the loop
Solution
Step 1: Check while loop condition safety
fast.next.next is accessed without confirming fast is not null, can cause error if fast is null.
Step 2: Correct condition to avoid runtime error
Condition should be while (fast && fast.next) to safely access fast.next.next.
Final Answer:
The while condition should check if fast and fast.next exist -> Option A
Quick Check:
Check fast and fast.next before accessing next nodes [OK]
Hint: Always check fast and fast.next before fast.next.next [OK]
Common Mistakes:
Not checking if fast is null before accessing fast.next.next
Using == instead of === (not critical here)
Moving slow pointer two steps incorrectly
5. Given a linked list where the last node points back to the head, how can you modify the detection function to also return the node where the loop starts?
function detectLoopStart(head) {
let slow = head;
let fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
// Loop detected
slow = head;
while (slow !== fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}
return null;
}
hard
A. The function returns the node where the loop starts by resetting slow to head and moving both pointers one step
B. The function returns true if a loop exists but does not find the start node
C. The function incorrectly moves fast pointer one step after detection
D. The function will always return null because slow and fast never meet
Solution
Step 1: Detect loop using two pointers
Slow and fast pointers meet inside the loop confirming a cycle.
Step 2: Find loop start node
Reset slow to head, move slow and fast one step at a time until they meet again; this node is loop start.
Final Answer:
The function returns the node where the loop starts by resetting slow to head and moving both pointers one step -> Option A
Quick Check:
Reset slow and move both pointers to find loop start [OK]
Hint: Reset slow to head after detection to find loop start [OK]
Common Mistakes:
Returning true instead of loop start node
Moving fast pointer incorrectly after detection
Assuming slow and fast never meet
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.