0
0
DSA Pythonprogramming~20 mins

Detect if a Linked List is Circular in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code detecting circular linked list?

Consider the following Python code that checks if a linked list is circular. What will it print?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

# Create nodes
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)

# Link nodes
node1.next = node2
node2.next = node3
node3.next = node1  # Circular link

print(is_circular(node1))
ARaises an exception
BFalse
CNone
DTrue
Attempts:
2 left
💡 Hint

Think about how the fast and slow pointers move and when they meet.

Predict Output
intermediate
2:00remaining
What does this code print for a non-circular linked list?

Given the code below, what will be the output?

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

# Create nodes
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)

# Link nodes
node1.next = node2
node2.next = node3
node3.next = None  # No circular link

print(is_circular(node1))
AFalse
BTrue
CNone
DRaises an exception
Attempts:
2 left
💡 Hint

Check if the fast pointer reaches the end of the list.

🔧 Debug
advanced
2:00remaining
Find the error in this circular linked list detection code

Identify the error in the following code that tries to detect if a linked list is circular.

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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
AThe condition 'if slow.next == fast.next' is incorrect; should be 'if slow == fast'
BThe fast pointer is not moving correctly; it should move one step at a time
CThe function does not handle empty list input
DThe slow pointer should start at head.next, not head
Attempts:
2 left
💡 Hint

Think about when the slow and fast pointers meet in a cycle.

🧠 Conceptual
advanced
2:00remaining
Why does Floyd's cycle detection algorithm use two pointers moving at different speeds?

Choose the best explanation for why Floyd's cycle detection algorithm uses a slow pointer and a fast pointer.

ATo ensure the fast pointer reaches the end of the list faster than the slow pointer
BTo detect a cycle by having the fast pointer lap the slow pointer inside the cycle
CTo reduce the memory usage by using two pointers instead of a hash set
DTo sort the linked list while detecting cycles
Attempts:
2 left
💡 Hint

Think about how the pointers meet if a cycle exists.

Predict Output
expert
2:00remaining
What is the output of this code with a single node circular linked list?

Analyze the code below and determine what it prints.

DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

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

# Single node circular list
node1 = Node(1)
node1.next = node1

print(is_circular(node1))
ANone
BFalse
CTrue
DRaises an exception
Attempts:
2 left
💡 Hint

Consider how the fast and slow pointers start and move in a single node circular list.