Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Create a Circular Singly Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Circular Singly Linked List after Insertions
What is the printed output of the circular singly linked list after inserting nodes with values 10, 20, and 30 in that order?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularSinglyLinkedList:
    def __init__(self):
        self.head = None

    def insert(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = new_node
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def print_list(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head
        result = []
        while True:
            result.append(str(temp.data))
            temp = temp.next
            if temp == self.head:
                break
        print(" -> ".join(result) + " -> (head)")

cll = CircularSinglyLinkedList()
cll.insert(10)
cll.insert(20)
cll.insert(30)
cll.print_list()
A10 -> 20 -> 30 -> (head)
B10 -> 20 -> 30 -> None
C30 -> 20 -> 10 -> (head)
DList is empty
Attempts:
2 left
💡 Hint
Remember that in a circular singly linked list, the last node points back to the head.
❓ Predict Output
intermediate
2:00remaining
Result of Deleting Head Node in Circular Singly Linked List
What is the printed output after deleting the head node from the circular singly linked list containing 5 -> 15 -> 25 -> (head)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularSinglyLinkedList:
    def __init__(self):
        self.head = None

    def insert(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = new_node
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def delete_head(self):
        if not self.head:
            return
        if self.head.next == self.head:
            self.head = None
            return
        temp = self.head
        while temp.next != self.head:
            temp = temp.next
        temp.next = self.head.next
        self.head = self.head.next

    def print_list(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head
        result = []
        while True:
            result.append(str(temp.data))
            temp = temp.next
            if temp == self.head:
                break
        print(" -> ".join(result) + " -> (head)")

cll = CircularSinglyLinkedList()
cll.insert(5)
cll.insert(15)
cll.insert(25)
cll.delete_head()
cll.print_list()
A5 -> 15 -> 25 -> (head)
B15 -> 25 -> (head)
C25 -> 15 -> (head)
DList is empty
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Circular Singly Linked List Insertion
What is the bug in the following insert method of a circular singly linked list?
DSA Python
def insert(self, data):
    new_node = Node(data)
    if not self.head:
        self.head = new_node
        new_node.next = None
    else:
        temp = self.head
        while temp.next != self.head:
            temp = temp.next
        temp.next = new_node
        new_node.next = self.head
Anew_node.next should point to self.head, but it is set to None when list is empty
BThe while loop condition should be temp.next == self.head instead of != self.head
CThe head should be updated to new_node after insertion
DThe method does not create a new node before insertion
Attempts:
2 left
💡 Hint
Check what happens when the list is empty and a new node is inserted.
❓ Predict Output
advanced
2:00remaining
Output after Traversing Circular Singly Linked List with One Node
What is the output of the print_list method when the circular singly linked list has only one node with value 42?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class CircularSinglyLinkedList:
    def __init__(self):
        self.head = None

    def insert(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            new_node.next = new_node
        else:
            temp = self.head
            while temp.next != self.head:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head

    def print_list(self):
        if not self.head:
            print("List is empty")
            return
        temp = self.head
        result = []
        while True:
            result.append(str(temp.data))
            temp = temp.next
            if temp == self.head:
                break
        print(" -> ".join(result) + " -> (head)")

cll = CircularSinglyLinkedList()
cll.insert(42)
cll.print_list()
A42
B42 -> None
CList is empty
D42 -> (head)
Attempts:
2 left
💡 Hint
A single node points to itself in a circular singly linked list.
🧠 Conceptual
expert
2:00remaining
Why Use Circular Singly Linked List Instead of Singly Linked List?
Which of the following is the main advantage of using a circular singly linked list over a regular singly linked list?
AIt automatically sorts the elements during insertion
BIt uses less memory than a singly linked list
CIt allows traversal from any node back to the head without null checks
DIt prevents duplicate values in the list
Attempts:
2 left
💡 Hint
Think about how the last node connects in a circular singly linked list.

Practice

(1/5)
1. What is a key characteristic of a circular singly linked list compared to a regular singly linked list?
easy
A. It uses an array internally instead of nodes.
B. The last node points back to the first node, forming a circle.
C. Each node has two pointers, one to the next and one to the previous node.
D. The list has a fixed size and cannot grow.

Solution

  1. Step 1: Understand the structure of a circular singly linked list

    In a circular singly linked list, the last node's next pointer points back to the first node, creating a loop.
  2. Step 2: Compare with other list types

    Unlike doubly linked lists, nodes have only one pointer. Unlike arrays, nodes are linked dynamically. The circular nature is unique to this structure where the last node points back to the first node, forming a circle.
  3. Final Answer:

    The last node points back to the first node, forming a circle. -> Option B
  4. Quick Check:

    Circular list loops back = C [OK]
Hint: Remember: circular means last node links to first [OK]
Common Mistakes:
  • Confusing circular with doubly linked list
  • Thinking nodes have two pointers
  • Assuming fixed size like arrays
2. Which of the following is the correct way to initialize an empty circular singly linked list in Python?
easy
A. head = None
B. head = Node(0); head.next = None
C. head = Node(0); head.next = head
D. head = []

Solution

  1. Step 1: Understand empty list initialization

    An empty circular singly linked list has no nodes, so head should be None.
  2. Step 2: Analyze other options

    head = Node(0); head.next = None sets next to None, which is not circular. head = Node(0); head.next = head creates a node pointing to itself, which is a single-node circular list, not empty. head = [] uses a list, not a linked list.
  3. Final Answer:

    head = None -> Option A
  4. Quick Check:

    Empty list head = None [OK]
Hint: Empty list means head points to nothing (None) [OK]
Common Mistakes:
  • Setting head.next to None in empty list
  • Creating a node when list is empty
  • Using Python list [] instead of nodes
3. What will be the output of the following code snippet?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(1)
second = Node(2)
third = Node(3)

head.next = second
second.next = third
third.next = head

current = head
result = []
for _ in range(4):
    result.append(current.data)
    current = current.next
print(result)
medium
A. [1, 2, 3, 3]
B. [1, 2, 3, None]
C. [1, 2, 3, 1]
D. RuntimeError due to infinite loop

Solution

  1. Step 1: Understand the circular link setup

    Nodes 1, 2, 3 are linked in a circle: 1->2->3->1.
  2. Step 2: Trace the loop for 4 iterations

    Starting at head (1), the loop collects data: 1, then moves to 2, then 3, then back to 1.
  3. Final Answer:

    [1, 2, 3, 1] -> Option C
  4. Quick Check:

    Loop of 3 nodes repeated once = [1,2,3,1] [OK]
Hint: Follow next pointers carefully for given loop count [OK]
Common Mistakes:
  • Assuming last node points to None
  • Stopping after 3 elements instead of 4
  • Confusing data values with node references
4. Identify the error in the following code that tries to create a circular singly linked list with one node:
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

head = Node(10)
head.next = None  # Intended to point to head for circular list
medium
A. Data value 10 is invalid for circular lists
B. Node class is missing a previous pointer
C. head should be initialized as None
D. head.next should point to head, not None

Solution

  1. Step 1: Check the pointer assignment for circularity

    For a circular singly linked list with one node, the node's next must point to itself (head), not None.
  2. Step 2: Analyze the given code

    The code sets head.next = None, breaking the circular link.
  3. Final Answer:

    head.next should point to head, not None -> Option D
  4. Quick Check:

    Single node circular list loops to itself [OK]
Hint: Single node circular list next points to itself [OK]
Common Mistakes:
  • Setting next to None instead of head
  • Confusing singly with doubly linked list
  • Thinking data value affects circularity
5. You want to insert a new node with value 4 at the end of an existing circular singly linked list with nodes 1 -> 2 -> 3 -> back to 1. Which of the following code snippets correctly performs this insertion?
hard
A. new_node = Node(4) current = head while current.next != head: current = current.next current.next = new_node new_node.next = head
B. new_node = Node(4) new_node.next = head head.next = new_node
C. new_node = Node(4) head.next = new_node new_node.next = None
D. new_node = Node(4) current = head while current.next != None: current = current.next current.next = new_node new_node.next = head

Solution

  1. Step 1: Locate the last node in the circular list

    Traverse nodes until current.next points back to head, identifying the last node.
  2. Step 2: Insert new node after last node and link it back to head

    Set last node's next to new_node, then new_node's next to head to maintain circularity.
  3. Final Answer:

    Traverse until current.next != head, then set current.next = new_node and new_node.next = head -> Option A
  4. Quick Check:

    Traverse till last node, link new node, maintain circle [OK]
Hint: Find last node by checking next != head, then insert [OK]
Common Mistakes:
  • Setting new_node.next to None breaking circularity
  • Not traversing to last node correctly
  • Assuming head.next is always last node
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