Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Insert at Beginning of Circular Linked List in DSA Python - Practice Problems & Challenges

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

Practice

(1/5)
1. What happens when you insert a new node at the beginning of a circular linked list?
easy
A. The new node replaces the last node in the list.
B. The new node becomes the head and points to the old head, maintaining the circular link.
C. The list breaks and becomes a linear linked list.
D. The new node is added at the end and does not affect the head.

Solution

  1. Step 1: Understand the circular linked list structure

    A circular linked list has its last node pointing back to the head node, forming a circle.
  2. Step 2: Insert new node at beginning

    The new node is linked to the current head, and the last node's next pointer is updated to this new node, making it the new head.
  3. Final Answer:

    The new node becomes the head and points to the old head, maintaining the circular link. -> Option B
  4. Quick Check:

    New head points to old head, last node points to new head [OK]
Hint: New head points to old head, last node points to new head [OK]
Common Mistakes:
  • Forgetting to update last node's next pointer
  • Breaking the circular link by not linking last node
  • Replacing the last node instead of adding new head
2. Which of the following is the correct syntax to create a new node and insert it at the beginning of a circular linked list in Python?
easy
A. new_node = Node(data); new_node.next = head; head = new_node
B. new_node = Node(data); last.next = head; head = new_node
C. new_node = Node(data); head.next = new_node; new_node.next = head
D. new_node = Node(data); new_node.next = head; last.next = new_node; head = new_node

Solution

  1. Step 1: Create new node and link to current head

    We create a new node and set its next pointer to the current head.
  2. Step 2: Update last node's next pointer and head

    Since it's circular, last node's next must point to new node, then update head to new node.
  3. Final Answer:

    new_node = Node(data); new_node.next = head; last.next = new_node; head = new_node -> Option D
  4. Quick Check:

    New node points to old head, last points to new node, head updated [OK]
Hint: Update last.next to new node before changing head [OK]
Common Mistakes:
  • Not updating last.next to new node
  • Changing head before updating last.next
  • Linking new node incorrectly
3. What will be the output of the following code snippet after inserting 10 at the beginning of the circular linked list?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_list(head):
    temp = head
    if head is not None:
        while True:
            print(temp.data, end=' -> ')
            temp = temp.next
            if temp == head:
                break
    print('null')

# Initial circular linked list: 20 -> 30 -> 40 -> null
head = Node(20)
second = Node(30)
third = Node(40)
head.next = second
second.next = third
third.next = head

# Insert 10 at beginning
new_node = Node(10)
new_node.next = head
third.next = new_node
head = new_node

print_list(head)
medium
A. 10 -> 30 -> 40 -> 20 -> null
B. 20 -> 30 -> 40 -> 10 -> null
C. 10 -> 20 -> 30 -> 40 -> null
D. 30 -> 40 -> 20 -> 10 -> null

Solution

  1. Step 1: Understand the initial list and insertion

    The list is 20 -> 30 -> 40 -> back to 20. Inserting 10 at beginning means new_node points to old head (20), and last node (40) points to new_node.
  2. Step 2: Trace the print_list function

    Starting from new head (10), it prints 10, then 20, then 30, then 40, then stops when it reaches head again.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null -> Option C
  4. Quick Check:

    New head 10 followed by old nodes in order [OK]
Hint: New head prints first, then old nodes in order [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Printing from old head instead of new head
  • Confusing order of nodes after insertion
4. Identify the error in the following code that inserts a new node at the beginning of a circular linked list:
def insert_begin(head, data):
    new_node = Node(data)
    new_node.next = head
    head = new_node
    return head
medium
A. The last node's next pointer is not updated to new_node, breaking the circular link.
B. The new_node.next should point to null instead of head.
C. The head should not be updated to new_node.
D. The function should not return head.

Solution

  1. Step 1: Check pointer updates for circular list

    In a circular linked list, the last node's next pointer must point to the head node to maintain the circle.
  2. Step 2: Identify missing update

    The code updates new_node.next and head but does not update the last node's next pointer to new_node, breaking the circle.
  3. Final Answer:

    The last node's next pointer is not updated to new_node, breaking the circular link. -> Option A
  4. Quick Check:

    Last node must point to new head to keep circle [OK]
Hint: Always update last.next to new head in circular list [OK]
Common Mistakes:
  • Ignoring last node pointer update
  • Setting new_node.next to null
  • Not returning updated head
5. Given a circular linked list with nodes 5 -> 15 -> 25 -> back to 5, write the steps to insert a new node with value 1 at the beginning and then print the list. What will be the printed output?
hard
A. 1 -> 5 -> 15 -> 25 -> null
B. 5 -> 15 -> 25 -> 1 -> null
C. 1 -> 15 -> 25 -> 5 -> null
D. 25 -> 5 -> 15 -> 1 -> null

Solution

  1. Step 1: Insert new node at beginning

    Create new node with value 1, set its next to current head (5), update last node (25) next to new node, then update head to new node.
  2. Step 2: Print the circular linked list

    Start from new head (1), print nodes in order: 1, 5, 15, 25, then stop when back to head.
  3. Final Answer:

    1 -> 5 -> 15 -> 25 -> null -> Option A
  4. Quick Check:

    New head 1 followed by old nodes in order [OK]
Hint: Update last.next before changing head to new node [OK]
Common Mistakes:
  • Not updating last node's next pointer
  • Printing from old head instead of new head
  • Confusing node order after insertion
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