Detect if a Linked List is Circular in DSA Typescript - Time & Space Complexity
We want to know how long it takes to check if a linked list loops back on itself.
How does the time needed grow as the list gets longer?
Analyze the time complexity of the following code snippet.
function isCircular(head: ListNode | null): boolean {
if (!head) return false;
let slow = head;
let fast = head.next;
while (fast !== null && fast.next !== null) {
if (slow === fast) return true;
slow = slow.next!;
fast = fast.next.next!;
}
return false;
}
This code uses two pointers moving at different speeds to detect a loop in the linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves two pointers through the list.
- How many times: At most, it runs once per node until pointers meet or list ends.
As the list grows, the number of steps grows roughly in a straight line with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 steps |
| 100 | Up to 100 steps |
| 1000 | Up to 1000 steps |
Pattern observation: The time grows linearly as the list gets longer.
Time Complexity: O(n)
This means the time to detect a loop grows in direct proportion to the number of nodes in the list.
[X] Wrong: "The fast pointer moves twice as fast, so the time is half the list length."
[OK] Correct: Even though the fast pointer moves faster, the slow pointer still moves step by step, so the total steps depend on the list size, not just half.
Understanding this method shows you can analyze pointer movement and loops, a key skill for many linked list problems.
"What if we used only one pointer moving one step at a time? How would the time complexity change?"
Practice
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 AQuick Check:
Circular linked list = last node points back [OK]
- Thinking circular means sorted
- Confusing empty list with circular
- Assuming single node list is always circular
Solution
Step 1: Understand Floyd's cycle detection
Use two pointers: slow moves 1 step, fast moves 2 steps. If they meet, list is circular.Step 2: Analyze code snippets
let slow = head; let fast = head; while(fast && fast.next) { slow = slow.next; fast = fast.next.next; if(slow === fast) return true; } return false; correctly implements this logic with proper conditions and pointer moves.Final Answer:
let slow = head; let fast = head; while(fast && fast.next) { slow = slow.next; fast = fast.next.next; if(slow === fast) return true; } return false; -> Option CQuick Check:
Two pointers meet means circular [OK]
- Moving fast pointer only one step
- Not checking fast and fast.next for null
- Returning false too early
class Node { constructor(public val: number, public next: Node | null = null) {} }
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = head.next;
function isCircular(head: Node | null): boolean {
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));Solution
Step 1: Understand the linked list structure
Nodes 1 -> 2 -> 3, then 3 points back to node 2, creating a loop.Step 2: Trace the two-pointer loop detection
Slow and fast pointers will meet inside the loop, so function returns true.Final Answer:
true -> Option AQuick Check:
Loop exists, so output is true [OK]
- Assuming no loop because list looks linear
- Confusing node references
- Ignoring pointer equality check
function isCircular(head: Node | null): boolean {
let slow = head;
let fast = head;
while (fast !== null) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) return true;
}
return false;
}Solution
Step 1: Analyze pointer movement safety
fast.next.next is accessed without checking if fast.next is null, risking runtime error.Step 2: Correct loop condition
Loop should check both fast and fast.next are not null before moving pointers to avoid errors.Final Answer:
The while loop should check fast and fast.next for null before moving pointers -> Option DQuick Check:
Check fast and fast.next to avoid errors [OK]
- Not checking fast.next before fast.next.next
- Moving slow pointer incorrectly
- Returning false too early
Which approach correctly finds the start of the loop?
Solution
Step 1: Detect cycle using two pointers
Use Floyd's algorithm to find if slow and fast pointers meet inside the loop.Step 2: Find loop start node
Reset one pointer to head, move both pointers one step at a time; their meeting point is the loop start.Final Answer:
After detecting the cycle, reset one pointer to head and move both pointers one step at a time until they meet; the meeting point is the start of the loop -> Option BQuick Check:
Reset pointer and move both to find loop start [OK]
- Assuming middle node is loop start
- Using node count instead of pointers
- Trying to reverse list to find loop
Ready to prove it?
Pass the test and this pattern moves unknown
