0
0
DSA Pythonprogramming~20 mins

Delete Node at Specific Position in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linked 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 node at position 2 in a linked list
What is the printed linked list after deleting the node at position 2 (0-based index)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, position):
    if position == 0:
        return head.next
    current = head
    count = 0
    while current is not None and count < position - 1:
        current = current.next
        count += 1
    if current is None or current.next is None:
        return head
    current.next = current.next.next
    return head

def print_list(head):
    current = head
    result = []
    while current:
        result.append(str(current.data))
        current = current.next
    print(' -> '.join(result) + ' -> null')

# Create linked list 10 -> 20 -> 30 -> 40 -> null
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)

head = delete_node(head, 2)
print_list(head)
A10 -> 20 -> 40 -> null
B10 -> 30 -> 40 -> null
C20 -> 30 -> 40 -> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember that position is zero-based and you need to remove the node at that position.
Predict Output
intermediate
2:00remaining
Result after deleting head node in a linked list
What is the printed linked list after deleting the node at position 0 (head node)?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, position):
    if position == 0:
        return head.next
    current = head
    count = 0
    while current is not None and count < position - 1:
        current = current.next
        count += 1
    if current is None or current.next is None:
        return head
    current.next = current.next.next
    return head

def print_list(head):
    current = head
    result = []
    while current:
        result.append(str(current.data))
        current = current.next
    print(' -> '.join(result) + ' -> null')

# Create linked list 5 -> 15 -> 25 -> null
head = Node(5)
head.next = Node(15)
head.next.next = Node(25)

head = delete_node(head, 0)
print_list(head)
A25 -> null
B15 -> 25 -> null
C5 -> 25 -> null
D5 -> 15 -> null
Attempts:
2 left
💡 Hint
Deleting position 0 means removing the head node.
Predict Output
advanced
2:00remaining
Output after deleting node at invalid position
What is the printed linked list after trying to delete node at position 5 in a list of length 3?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, position):
    if position == 0:
        return head.next
    current = head
    count = 0
    while current is not None and count < position - 1:
        current = current.next
        count += 1
    if current is None or current.next is None:
        return head
    current.next = current.next.next
    return head

def print_list(head):
    current = head
    result = []
    while current:
        result.append(str(current.data))
        current = current.next
    print(' -> '.join(result) + ' -> null')

# Create linked list 1 -> 2 -> 3 -> null
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)

head = delete_node(head, 5)
print_list(head)
Anull
B1 -> 3 -> null
C2 -> 3 -> null
D1 -> 2 -> 3 -> null
Attempts:
2 left
💡 Hint
Deleting a node at a position outside the list length should not change the list.
🔧 Debug
advanced
2:00remaining
Identify the error in delete_node function
What error will this code raise when deleting node at position 3 in a 3-node list?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, position):
    if position == 0:
        return head.next
    current = head
    count = 0
    while current.next is not None and count < position - 1:
        current = current.next
        count += 1
    current.next = current.next.next
    return head

def print_list(head):
    current = head
    result = []
    while current:
        result.append(str(current.data))
        current = current.next
    print(' -> '.join(result) + ' -> null')

# Create linked list 7 -> 8 -> 9 -> null
head = Node(7)
head.next = Node(8)
head.next.next = Node(9)

head = delete_node(head, 3)
print_list(head)
AAttributeError
BIndexError
CNoneType AttributeError
DNo error, prints 7 -> 8 -> 9 -> null
Attempts:
2 left
💡 Hint
Check what happens when current.next is None and you try to access current.next.next.
🚀 Application
expert
3:00remaining
Number of nodes after multiple deletions
Given the linked list 1 -> 2 -> 3 -> 4 -> 5 -> null, after deleting nodes at positions 1, 2, and 0 in that order, how many nodes remain?
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, position):
    if position == 0:
        return head.next
    current = head
    count = 0
    while current is not None and count < position - 1:
        current = current.next
        count += 1
    if current is None or current.next is None:
        return head
    current.next = current.next.next
    return head

# Create linked list 1 -> 2 -> 3 -> 4 -> 5 -> null
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)

head = delete_node(head, 1)  # delete node at position 1
head = delete_node(head, 2)  # delete node at position 2
head = delete_node(head, 0)  # delete node at position 0

# Count nodes
count = 0
current = head
while current:
    count += 1
    current = current.next
print(count)
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Perform each deletion step by step and count nodes after all deletions.