0
0
DSA Pythonprogramming~20 mins

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

Choose your learning style9 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.