Bird
Raised Fist0
DSA Pythonprogramming~20 mins

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

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

Practice

(1/5)
1. What happens when you delete a node at position 0 in a singly linked list?
easy
A. The last node is removed
B. The list remains unchanged
C. The head node is removed and the next node becomes the new head
D. All nodes are deleted

Solution

  1. Step 1: Understand position 0 in linked list

    Position 0 refers to the head node of the list.
  2. Step 2: Effect of deleting head node

    Deleting the head means the next node becomes the new head.
  3. Final Answer:

    The head node is removed and the next node becomes the new head -> Option C
  4. Quick Check:

    Deleting position 0 removes head [OK]
Hint: Deleting position 0 always removes the head node [OK]
Common Mistakes:
  • Thinking last node is deleted at position 0
  • Assuming list stays same after deletion
  • Deleting all nodes instead of one
2. Which of the following is the correct syntax to delete a node at position pos in a singly linked list in Python?
easy
A. head = head.next if pos == 1 else head
B. if pos == 0: head = head.next
C. head.next = head if pos == 0 else head.next
D. head = head.prev if pos == 0 else head.next

Solution

  1. Step 1: Check deletion at position 0

    Deleting at position 0 means moving head to next node.
  2. Step 2: Verify syntax correctness

    if pos == 0: head = head.next correctly assigns head to head.next when pos is 0.
  3. Final Answer:

    if pos == 0: head = head.next -> Option B
  4. Quick Check:

    Correct syntax for deleting head node [OK]
Hint: Deleting head means head = head.next [OK]
Common Mistakes:
  • Using head.prev which doesn't exist in singly linked list
  • Wrong condition for position check
  • Assigning head incorrectly
3. What will be the linked list after deleting the node at position 2 from the list: 10 -> 20 -> 30 -> 40 -> null?
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_node(head, pos):
    if pos == 0:
        return head.next
    current = head
    for _ in range(pos - 1):
        current = current.next
    current.next = current.next.next
    return head

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
current = head
result = []
while current:
    result.append(current.data)
    current = current.next
print(result)
medium
A. [10, 20, 40]
B. [10, 30, 40]
C. [20, 30, 40]
D. [10, 20, 30, 40]

Solution

  1. Step 1: Identify node at position 2

    Position 2 node has value 30 in the list 10 -> 20 -> 30 -> 40.
  2. Step 2: Delete node at position 2

    Node 20's next pointer is updated to node 40, skipping node 30.
  3. Final Answer:

    [10, 20, 40] -> Option A
  4. Quick Check:

    Deleted node 30 at position 2 [OK]
Hint: Skip node at position by linking previous to next [OK]
Common Mistakes:
  • Deleting wrong node due to off-by-one error
  • Not updating next pointer correctly
  • Returning wrong head
4. Find the error in the following code snippet that deletes a node at position pos in a singly linked list:
def delete_node(head, pos):
    if pos == 0:
        head = head.next
    current = head
    for _ in range(pos - 1):
        current = current.next
    current.next = current.next.next
    return head
medium
A. No error, code is correct
B. Loop runs one time too many
C. current.next.next may cause AttributeError if current.next is None
D. Missing return after deleting head node

Solution

  1. Step 1: Check deletion at position 0

    When pos is 0, head is updated but not returned immediately.
  2. Step 2: Effect of missing return

    Code continues and tries to access current.next causing errors or wrong behavior.
  3. Final Answer:

    Missing return after deleting head node -> Option D
  4. Quick Check:

    Return immediately after deleting head [OK]
Hint: Return immediately after deleting head node [OK]
Common Mistakes:
  • Not returning head after deleting first node
  • Ignoring edge cases when list is empty
  • Assuming current.next always exists
5. Given a singly linked list 5 -> 10 -> 15 -> 20 -> 25 -> null, write the resulting list after deleting nodes at positions 0, 2, and then 1 in that order. What is the final list?
hard
A. [10, 25]
B. [10, 15, 25]
C. [10, 20]
D. [5, 15, 25]

Solution

  1. Step 1: Delete node at position 0

    Deleting head (5), list becomes 10 -> 15 -> 20 -> 25.
  2. Step 2: Delete node at position 2

    Node at position 2 is 20, deleting it results in 10 -> 15 -> 25.
  3. Step 3: Delete node at position 1

    Node at position 1 is 15, deleting it results in 10 -> 25.
  4. Final Answer:

    [10, 25] -> Option A
  5. Quick Check:

    Sequential deletions update list correctly [OK]
Hint: Delete nodes stepwise, update positions after each deletion [OK]
Common Mistakes:
  • Not updating list after each deletion
  • Confusing positions after deletions
  • Deleting wrong nodes due to static indexing
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