What does it mean for a linked list to be circular?
A linked list is circular if the last node points back to one of the previous nodes, creating a loop instead of ending with null.
Click to reveal answer
beginner
Which two pointers are commonly used to detect a cycle in a linked list?
The 'slow' pointer moves one step at a time, and the 'fast' pointer moves two steps at a time. If they meet, the list is circular.
Click to reveal answer
intermediate
Why does the fast pointer moving two steps help detect a cycle?
Because if there is a cycle, the fast pointer will eventually catch up to the slow pointer inside the loop, proving the list is circular.
Click to reveal answer
intermediate
What is the time complexity of detecting a cycle in a linked list using the two-pointer method?
The time complexity is O(n), where n is the number of nodes in the list, because each pointer moves through the list at most once.
Click to reveal answer
beginner
What should the function return if the linked list is not circular?
The function should return false, indicating no cycle was found and the list ends with a null pointer.
Click to reveal answer
What does the fast pointer do in cycle detection?
AMoves one step at a time
BMoves two steps at a time
CMoves three steps at a time
DDoes not move
✗ Incorrect
The fast pointer moves two steps at a time to catch the slow pointer if a cycle exists.
If the slow and fast pointers never meet, what does it mean?
AThe list is not circular
BThe list is circular
CThe list is empty
DThe pointers are broken
✗ Incorrect
If the pointers never meet, the list ends with null and is not circular.
What is the initial position of the slow and fast pointers in cycle detection?
ABoth start at null
BSlow starts at head, fast at second node
CBoth start at the head node
DSlow starts at null, fast at head
✗ Incorrect
Both pointers start at the head node before moving through the list.
What is the main advantage of using two pointers for cycle detection?
ADetects cycle in O(n) time without extra space
BSlower than checking each node
CUses extra memory
DOnly works for doubly linked lists
✗ Incorrect
Two pointers detect cycles efficiently in linear time without extra memory.
What happens if the linked list has only one node pointing to itself?
AIt is not circular
BIt is empty
CIt causes an error
DIt is circular
✗ Incorrect
A single node pointing to itself forms a cycle, so the list is circular.
Explain how the two-pointer method detects a cycle in a linked list.
Think about how fast pointer can catch slow pointer in a loop.
You got /5 concepts.
Describe what a circular linked list looks like and how it differs from a normal linked list.
Imagine a train track that loops back on itself.
You got /4 concepts.
Practice
(1/5)
1. What does it mean if a linked list is circular?
easy
A. The linked list nodes are sorted in ascending order
B. The last node points back to one of the previous nodes, forming a loop
C. The linked list has no nodes
D. The linked list contains only one node
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 B
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
Confusing empty list with circular
Assuming single node list is always circular
2. Which of the following Java code snippets correctly checks if a linked list is circular using two pointers?
easy
A. while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; }
B. while (slow != null) { slow = slow.next; if (slow == fast) return true; }
C. for (int i = 0; i < size; i++) { if (node == null) return false; node = node.next; }
D. if (head == null) return true; else return false;
Solution
Step 1: Identify Floyd's cycle detection logic
Two pointers move at different speeds: slow moves one step, fast moves two steps.
Step 2: Check for meeting point
If slow and fast pointers meet, the list is circular; otherwise, if fast reaches null, it's not circular.
Final Answer:
while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } -> Option A
Quick Check:
Two pointers meet = circular [OK]
Hint: Use two pointers moving at different speeds [OK]
Common Mistakes:
Not checking fast.next for null
Using only one pointer to detect loop
Returning true when list is empty
3. What will be the output of the following Java code snippet?
class Node { int data; Node next; Node(int d) { data = d; next = null; } } public class Test { public static boolean isCircular(Node head) { if (head == null) return false; Node slow = head, fast = head.next; while (fast != null && fast.next != null) { if (slow == fast) return true; slow = slow.next; fast = fast.next.next; } return false; } public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = head; // circular link System.out.println(isCircular(head)); } }
medium
A. Runtime exception
B. false
C. Compilation error
D. true
Solution
Step 1: Understand the linked list structure
The list has nodes 1 -> 2 -> 3, and then 3 points back to 1, making it circular.
Step 2: Trace the isCircular method
Slow starts at 1, fast at 2. They move until they meet because of the loop, so method returns true.
Final Answer:
true -> Option D
Quick Check:
Loop present = true [OK]
Hint: If fast and slow meet, list is circular [OK]
Common Mistakes:
Starting fast pointer incorrectly
Not creating circular link properly
Confusing output with false
4. Identify the bug in this code snippet that tries to detect if a linked list is circular:
public boolean isCircular(Node head) { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow.next == fast.next) return true; } return false; }
medium
A. slow pointer should start at head.next
B. fast pointer should move only one step at a time
C. The condition should compare slow and fast, not slow.next and fast.next
D. The loop condition should be while (slow != null)
Solution
Step 1: Analyze pointer comparison
The code compares slow.next and fast.next, but to detect loop, slow and fast themselves must be compared.
Step 2: Correct the comparison
Change if condition to if (slow == fast) to detect when pointers meet inside the loop.
Final Answer:
The condition should compare slow and fast, not slow.next and fast.next -> Option C
Quick Check:
Compare slow and fast pointers directly [OK]
Hint: Compare slow and fast nodes directly to detect loop [OK]
Common Mistakes:
Comparing next nodes instead of current nodes
Moving fast pointer incorrectly
Wrong loop condition
5. Given a linked list where the last node points to the head node, how can you modify the detection method to also return the length of the loop?
hard
A. After detecting the loop with slow == fast, keep slow fixed and move fast until it meets slow again, counting steps
B. Count nodes from head to tail without checking for loop
C. Return the total number of nodes visited before fast reaches null
D. Use a hash set to store nodes and count unique nodes only
Solution
Step 1: Detect loop using two pointers
Use Floyd's cycle detection to find when slow equals fast inside the loop.
Step 2: Count loop length
Keep slow pointer fixed, move fast pointer one step at a time, incrementing count until fast meets slow again.
Final Answer:
After detecting the loop with slow == fast, keep slow fixed and move fast until it meets slow again, counting steps -> Option A
Quick Check:
Count steps in loop after detection [OK]
Hint: Count nodes in loop by moving one pointer until it meets again [OK]
Common Mistakes:
Counting nodes before loop detection
Using total nodes instead of loop length
Not moving pointer correctly to count loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.