Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Detect if a Linked List is Circular in DSA Python - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the slow pointer at the head of the linked list.

DSA Python
slow = [1]
Drag options to blanks, or click blank then click option'
Aslow
BNone
Ctail
Dhead
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing slow to None will cause the loop to never start.
Using tail instead of head will skip the list start.
2fill in blank
medium

Complete the code to move the fast pointer two steps ahead in the linked list.

DSA Python
fast = fast[1]next[2]next
Drag options to blanks, or click blank then click option'
A.
B->
C::
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' is syntax from other languages like C++.
Using commas or double colons are invalid here.
3fill in blank
hard

Fix the error in the condition to check if the fast pointer or its next node is None.

DSA Python
while fast is not [1] and fast.next is not None:
Drag options to blanks, or click blank then click option'
Ahead
BNone
Cfast
Dslow
Attempts:
3 left
💡 Hint
Common Mistakes
Checking against head or slow is incorrect here.
Using variable names instead of None causes errors.
4fill in blank
hard

Fill both blanks to correctly move slow and fast pointers inside the loop.

DSA Python
slow = slow[1]next
fast = fast[2]next[3]next
Drag options to blanks, or click blank then click option'
A.
B->
C::
D,
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->' or other symbols causes syntax errors.
Using commas or double colons is invalid.
5fill in blank
hard

Fill all three blanks to return True if slow meets fast, else False after the loop.

DSA Python
if slow == [1]:
    return [2]
return [3]
Drag options to blanks, or click blank then click option'
Afast
BTrue
CFalse
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Returning None instead of False when no cycle is found.
Comparing slow with wrong variable.

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 linked list has no nodes
C. The linked list has only one node
D. The last node points back to one of the previous nodes, forming a loop

Solution

  1. Step 1: Understand linked list structure

    A linked list is a chain of nodes where each node points to the next node.
  2. Step 2: Define circular linked list

    If the last node points back to any previous node, it creates a loop, making the list circular.
  3. Final Answer:

    The last node points back to one of the previous nodes, forming a loop -> Option D
  4. Quick Check:

    Circular linked list means loop exists [OK]
Hint: Circular means last node links back inside list [OK]
Common Mistakes:
  • Thinking circular means sorted
  • Confusing empty list with circular
  • Assuming single node list is always circular
2. Which of the following Python code snippets correctly checks if a linked list is circular?
easy
A. def is_circular(head): if not head: return False slow = head fast = head.next while fast and fast.next: if slow == fast: return True slow = slow.next fast = fast.next.next return False
B. def is_circular(head): current = head while current: current = current.next return True
C. def is_circular(head): return head.next == None
D. def is_circular(head): return head == None

Solution

  1. Step 1: Understand Floyd's cycle detection

    Using two pointers (slow and fast) moving at different speeds detects loops if they meet.
  2. Step 2: Analyze code correctness

    def is_circular(head): if not head: return False slow = head fast = head.next while fast and fast.next: if slow == fast: return True slow = slow.next fast = fast.next.next return False uses slow and fast pointers correctly and returns True if they meet, else False.
  3. Final Answer:

    Code using slow and fast pointers to detect loop -> Option A
  4. Quick Check:

    Floyd's cycle detection = def is_circular(head): if not head: return False slow = head fast = head.next while fast and fast.next: if slow == fast: return True slow = slow.next fast = fast.next.next return False [OK]
Hint: Use two pointers moving at different speeds [OK]
Common Mistakes:
  • Not checking fast pointer's next before accessing
  • Assuming reaching None means circular
  • Returning True without loop detection
3. What will be the output of the following code snippet?
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.next = third
third.next = head  # Creates a circular linked list

def is_circular(head):
    slow = head
    fast = head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False

print(is_circular(head))
medium
A. True
B. False
C. None
D. Runtime Error

Solution

  1. Step 1: Understand list structure

    Nodes 1->2->3, and third points back to head, making a loop.
  2. Step 2: Trace Floyd's cycle detection

    Slow and fast pointers will eventually meet inside the loop, so function returns True.
  3. Final Answer:

    True -> Option A
  4. Quick Check:

    Loop detected = True [OK]
Hint: Loop means slow and fast pointers meet [OK]
Common Mistakes:
  • Ignoring the loop created by third.next = head
  • Assuming function returns False by default
  • Confusing None return with False
4. Find the error in the following code that tries to detect if a linked list is circular:
def is_circular(head):
    slow = head
    fast = head
    while fast.next and fast.next.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True
    return False
medium
A. The function should return True outside the loop unconditionally
B. The slow pointer should move two steps instead of one
C. The while loop condition should check if fast and fast.next exist
D. The fast pointer should start at head.next

Solution

  1. Step 1: Analyze while loop condition

    Current condition checks fast.next and fast.next.next but does not check if fast itself is None.
  2. Step 2: Correct condition to avoid runtime error

    Should check if fast and fast.next exist before accessing fast.next.next to prevent errors.
  3. Final Answer:

    The while loop condition should check if fast and fast.next exist -> Option C
  4. Quick Check:

    Check fast and fast.next to avoid errors [OK]
Hint: Always check pointer exists before accessing next [OK]
Common Mistakes:
  • Not checking if fast is None before fast.next
  • Moving slow pointer incorrectly
  • Returning True too early
5. You have a linked list where the last node points to the third node, making it circular. How would you modify the detection function to also return the node where the loop starts?
hard
A. Return the node where slow and fast first meet during detection
B. Use Floyd's algorithm: after detecting loop, reset slow to head and move slow and fast one step until they meet; that node is loop start
C. Return the head node always as loop start
D. Use a hash set to store visited nodes and return the first repeated node

Solution

  1. Step 1: Detect loop using Floyd's cycle detection

    Use slow and fast pointers to find if loop exists by meeting inside the loop.
  2. Step 2: Find loop start node

    Reset slow to head, then move slow and fast one step at a time; the node where they meet is the loop start.
  3. Final Answer:

    Use Floyd's algorithm with reset slow pointer to find loop start -> Option B
  4. Quick Check:

    Reset slow and move both pointers to find loop start [OK]
Hint: After loop detection, reset slow to head and move both one step [OK]
Common Mistakes:
  • Returning meeting point as loop start without reset
  • Always returning head as loop start
  • Not handling loop detection before finding start
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