Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Delete a Node from Circular Linked List in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Circular List Deletion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting a node with value 3
What is the printed state of the circular linked list after deleting the node with value 3?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_circular_list(head):
    if not head:
        print("Empty")
        return
    curr = head
    result = []
    while True:
        result.append(str(curr.data))
        curr = curr.next
        if curr == head:
            break
    print(" -> ".join(result) + " -> null")

def delete_node(head, key):
    if not head:
        return None
    curr = head
    prev = None
    while True:
        if curr.data == key:
            if prev:
                prev.next = curr.next
            else:
                # Deleting head node
                tail = head
                while tail.next != head:
                    tail = tail.next
                if tail == head:
                    return None
                tail.next = head.next
                head = head.next
            return head
        prev = curr
        curr = curr.next
        if curr == head:
            break
    return head

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = head

head = delete_node(head, 3)
print_circular_list(head)
A1 -> 2 -> 3 -> 4 -> null
B1 -> 4 -> null
C2 -> 3 -> 4 -> null
D1 -> 2 -> 4 -> null
Attempts:
2 left
💡 Hint
Remember to update the links correctly when deleting a node in a circular list.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the head node
What is the printed state of the circular linked list after deleting the node with value 1 (the head)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_circular_list(head):
    if not head:
        print("Empty")
        return
    curr = head
    result = []
    while True:
        result.append(str(curr.data))
        curr = curr.next
        if curr == head:
            break
    print(" -> ".join(result) + " -> null")

def delete_node(head, key):
    if not head:
        return None
    curr = head
    prev = None
    while True:
        if curr.data == key:
            if prev:
                prev.next = curr.next
            else:
                # Deleting head node
                tail = head
                while tail.next != head:
                    tail = tail.next
                if tail == head:
                    return None
                tail.next = head.next
                head = head.next
            return head
        prev = curr
        curr = curr.next
        if curr == head:
            break
    return head

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = head

head = delete_node(head, 1)
print_circular_list(head)
A3 -> 4 -> 1 -> null
B2 -> 3 -> 4 -> null
C1 -> 2 -> 3 -> 4 -> null
D4 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting the head requires updating the tail's next pointer.
🔧 Debug
advanced
2:00remaining
Identify the error in this delete function
What error will this code raise when trying to delete a node from a circular linked list?
DSA Python
def delete_node(head, key):
    if not head:
        return None
    curr = head
    prev = None
    while curr.data != key:
        prev = curr
        curr = curr.next
    if prev:
        prev.next = curr.next
    else:
        tail = head
        while tail.next != head:
            tail = tail.next
        tail.next = head.next
        head = head.next
    return head

head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = head

head = delete_node(head, 4)
ANoneType error
BKeyError
CInfinite loop
DAttributeError
Attempts:
2 left
💡 Hint
Consider what happens if the key is not found in the list.
🧠 Conceptual
advanced
1:30remaining
Why update tail's next pointer when deleting head?
In a circular linked list, why must we update the tail node's next pointer when deleting the head node?
ABecause the tail node's next pointer always points to the head, so it must point to the new head after deletion.
BBecause the tail node stores the head's data and needs updating.
CBecause the tail node is deleted along with the head node.
DBecause the tail node's next pointer points to None and must be reset.
Attempts:
2 left
💡 Hint
Think about the circular nature of the list and how nodes connect.
🚀 Application
expert
3:00remaining
Resulting list after multiple deletions
Given a circular linked list with nodes 10 -> 20 -> 30 -> 40 -> 50 -> null, what is the printed list after deleting nodes with values 10, 30, and 50 in that order?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_circular_list(head):
    if not head:
        print("Empty")
        return
    curr = head
    result = []
    while True:
        result.append(str(curr.data))
        curr = curr.next
        if curr == head:
            break
    print(" -> ".join(result) + " -> null")

def delete_node(head, key):
    if not head:
        return None
    curr = head
    prev = None
    while True:
        if curr.data == key:
            if prev:
                prev.next = curr.next
            else:
                tail = head
                while tail.next != head:
                    tail = tail.next
                if tail == head:
                    return None
                tail.next = head.next
                head = head.next
            return head
        prev = curr
        curr = curr.next
        if curr == head:
            break
    return head

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)
head.next.next.next.next = Node(50)
head.next.next.next.next.next = head

head = delete_node(head, 10)
head = delete_node(head, 30)
head = delete_node(head, 50)
print_circular_list(head)
A20 -> 40 -> null
B20 -> 40 -> 50 -> null
C10 -> 20 -> 40 -> null
D20 -> 30 -> 40 -> null
Attempts:
2 left
💡 Hint
Delete nodes one by one and update the head accordingly.

Practice

(1/5)
1. What happens when you delete the head node from a circular linked list with multiple nodes?
easy
A. The head moves to the next node and the list remains circular
B. The list becomes empty
C. The last node is deleted instead
D. The list breaks and becomes linear

Solution

  1. Step 1: Understand the head deletion in circular linked list

    When the head node is deleted, the next node becomes the new head.
  2. Step 2: Maintain circular link

    The last node's next pointer is updated to point to the new head, keeping the list circular.
  3. Final Answer:

    The head moves to the next node and the list remains circular -> Option A
  4. Quick Check:

    Head deletion updates head and keeps circular [OK]
Hint: Deleting head shifts head to next, list stays circular [OK]
Common Mistakes:
  • Assuming list becomes empty after head deletion
  • Not updating last node's next pointer
  • Breaking circular link accidentally
2. Which of the following is the correct syntax to delete a node with value `key` from a circular linked list in Python?
easy
A. def deleteNode(head, key): if head.data == key: # delete head logic
B. def deleteNode(head, key): while head.data != key: head = head.next
C. def deleteNode(head, key): if head is None: return None
D. def deleteNode(head, key): head = None return head

Solution

  1. Step 1: Check for empty list

    Before deleting, check if the list is empty (head is None) to avoid errors.
  2. Step 2: Return None if list is empty

    If head is None, return None as there is nothing to delete.
  3. Final Answer:

    def deleteNode(head, key): if head is None: return None -> Option C
  4. Quick Check:

    Empty list check is essential before deletion [OK]
Hint: Always check if list is empty before deleting [OK]
Common Mistakes:
  • Not checking if head is None before deletion
  • Incorrectly looping without exit condition
  • Setting head to None without condition
3. What will be the output list after deleting node with value 3 from this circular linked list: 1 -> 3 -> 5 -> 7 -> (back to 1)?
medium
A. 1 -> 5 -> 7 -> 3 -> (back to 1)
B. 1 -> 5 -> 7 -> (back to 1)
C. 1 -> 3 -> 5 -> 7 -> (back to 1)
D. 3 -> 5 -> 7 -> (back to 3)

Solution

  1. Step 1: Locate node with value 3

    Traverse the list to find node with value 3 between 1 and 5.
  2. Step 2: Remove node 3 and link 1 to 5

    Update node 1's next pointer to node 5, skipping node 3.
  3. Final Answer:

    1 -> 5 -> 7 -> (back to 1) -> Option B
  4. Quick Check:

    Deleted 3, linked 1 to 5 [OK]
Hint: Skip the node with key, link previous to next [OK]
Common Mistakes:
  • Not updating previous node's next pointer
  • Deleting wrong node or not deleting at all
  • Breaking circular link after deletion
4. Given this code snippet to delete a node from a circular linked list, what is the bug?
def deleteNode(head, key):
    if head is None:
        return None
    curr = head
    while curr.next != head:
        if curr.next.data == key:
            curr.next = curr.next.next
            return head
        curr = curr.next
    return head
medium
A. Does not handle deletion if head node contains the key
B. Infinite loop due to missing break
C. Deletes all nodes instead of one
D. Returns None instead of head

Solution

  1. Step 1: Analyze deletion logic

    The code checks nodes after head but never checks if head itself has the key.
  2. Step 2: Identify missing head deletion handling

    If head contains the key, this code will not delete it, causing incorrect behavior.
  3. Final Answer:

    Does not handle deletion if head node contains the key -> Option A
  4. Quick Check:

    Head node deletion must be handled separately [OK]
Hint: Always check if head node matches key before loop [OK]
Common Mistakes:
  • Ignoring head node in deletion logic
  • Assuming loop covers all nodes including head
  • Not updating head pointer after deletion
5. You have a circular linked list with only one node containing value 10. What should be the list after deleting node with value 10?
hard
A. List with node 10 pointing to None
B. List with node 10 pointing to itself
C. List unchanged
D. Empty list (None)

Solution

  1. Step 1: Identify single node list condition

    When the list has only one node, head.next points to head itself.
  2. Step 2: Delete the only node

    Deleting this node means the list becomes empty, so head should be None.
  3. Final Answer:

    Empty list (None) -> Option D
  4. Quick Check:

    Single node deletion empties the list [OK]
Hint: Deleting sole node results in empty list (None) [OK]
Common Mistakes:
  • Leaving node pointing to itself after deletion
  • Not setting head to None
  • Assuming list still has one 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