0
0
DSA Pythonprogramming~20 mins

Insert at Beginning of Circular Linked List 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
Output of inserting at beginning in circular linked list
What is the printed state of the circular linked list after inserting 10 at the beginning?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

    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) + " -> null")

cll = CircularLinkedList()
cll.insert_at_beginning(20)
cll.insert_at_beginning(30)
cll.insert_at_beginning(10)
cll.print_list()
A30 -> 20 -> 10 -> null
B20 -> 30 -> 10 -> null
C10 -> 30 -> 20 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember, inserting at beginning means new node becomes the head and points to the old head.
Predict Output
intermediate
2:00remaining
Output after inserting into empty circular linked list
What is the printed state of the circular linked list after inserting 5 into an empty list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

    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) + " -> null")

cll = CircularLinkedList()
cll.insert_at_beginning(5)
cll.print_list()
A5 -> null
B5 -> 5 -> null
CList is empty
D5 -> 0 -> null
Attempts:
2 left
💡 Hint
When list is empty, new node points to itself.
🔧 Debug
advanced
2:00remaining
Identify the bug in insert_at_beginning method
What error will this code raise when inserting into a circular linked list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

    def insert_at_beginning(self, data):
        new_node = Node(data)
        if self.head is None:
            new_node.next = new_node
            self.head = new_node
        else:
            temp = self.head
            while temp.next != None:
                temp = temp.next
            temp.next = new_node
            new_node.next = self.head
            self.head = new_node

cll = CircularLinkedList()
cll.insert_at_beginning(1)
cll.insert_at_beginning(2)
AAttributeError: 'NoneType' object has no attribute 'next'
BTypeError: unsupported operand type(s)
CNo error, works fine
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition for finding last node in circular linked list.
Predict Output
advanced
2:00remaining
Resulting list after multiple insertions at beginning
What is the printed state of the circular linked list after these insertions?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

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

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

    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) + " -> null")

cll = CircularLinkedList()
cll.insert_at_beginning(1)
cll.insert_at_beginning(2)
cll.insert_at_beginning(3)
cll.insert_at_beginning(4)
cll.print_list()
A4 -> 1 -> 2 -> 3 -> null
B4 -> 3 -> 2 -> 1 -> null
C1 -> 4 -> 3 -> 2 -> null
D1 -> 2 -> 3 -> 4 -> null
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
🧠 Conceptual
expert
2:00remaining
Why update last node's next pointer when inserting at beginning?
In a circular linked list, why must we update the last node's next pointer when inserting a new node at the beginning?
ATo maintain the circular link so the last node points to the new head
BTo avoid memory leaks by freeing the old head node
CTo reverse the list order after insertion
DTo make the new node point to None indicating end of list
Attempts:
2 left
💡 Hint
Think about what makes the list circular.