0
0
DSA Pythonprogramming~20 mins

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

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