Bird
Raised Fist0
DSA Pythonprogramming~5 mins

Detect if a Linked List is Circular in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does it mean for a linked list to be circular?
A linked list is circular if the last node points back to any previous node in the list, creating a loop instead of ending with null.
Click to reveal answer
intermediate
Which simple method can detect a circular linked list by using two pointers?
The Floyd’s Cycle-Finding Algorithm uses two pointers moving at different speeds (slow and fast). If they meet, the list is circular.
Click to reveal answer
beginner
What is the role of the slow and fast pointers in detecting a circular linked list?
The slow pointer moves one step at a time, and the fast pointer moves two steps. If the list is circular, they will eventually meet inside the loop.
Click to reveal answer
beginner
Why can't we just check if the next pointer is null to detect circular linked lists?
Because in a circular linked list, the next pointer never becomes null; it loops back to a previous node, so checking for null alone won't detect the loop.
Click to reveal answer
intermediate
What is the time complexity of Floyd’s Cycle-Finding Algorithm for detecting a circular linked list?
The time complexity is O(n), where n is the number of nodes, because each pointer moves through the list at most a few times.
Click to reveal answer
What happens if a linked list is not circular when using Floyd’s Cycle-Finding Algorithm?
AThe algorithm runs forever
BThe slow and fast pointers meet at some node
CThe slow pointer reaches null first
DThe fast pointer reaches null and the list is not circular
In Floyd’s Cycle-Finding Algorithm, how many steps does the fast pointer move compared to the slow pointer?
ATwo steps
BOne step
CThree steps
DSame number of steps
What is the main sign that a linked list is circular when using two pointers?
AThe slow pointer reaches null
BThe slow and fast pointers meet at the same node
CThe fast pointer reaches null
DThe list length is zero
Which of these is NOT a characteristic of a circular linked list?
ANo node has a null next pointer
BContains a loop
CLast node points to null
DTraversal can continue indefinitely
What is the space complexity of Floyd’s Cycle-Finding Algorithm?
AO(1)
BO(log n)
CO(n)
DO(n^2)
Explain how Floyd’s Cycle-Finding Algorithm detects a circular linked list step-by-step.
Think about how the pointers move and what it means when they meet.
You got /4 concepts.
    Describe why a circular linked list can cause problems if not detected.
    Imagine walking in a circle without stopping.
    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 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