0
0
DSA Pythonprogramming

Delete Node at Specific Position in DSA Python

Choose your learning style9 modes available
Mental Model
To remove a node at a certain position, we find the node just before it and change its pointer to skip the node to delete.
Analogy: Imagine a chain of paper clips linked together. To remove one clip in the middle, you unhook the clip before it and link it directly to the clip after the one you want to remove.
Head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
Positions: 1    2    3    4    5
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> 5, delete node at position 3
Goal: Remove the node at position 3 and keep the list connected
Step 1: Check if position is 1 (head), it is not
Head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
Why: If position is 1, we remove the head differently
Step 2: Start at head (node 1), move to node before position 3 (node 2)
Head -> 1 -> [curr->2] -> 3 -> 4 -> 5 -> null
Why: We need the node before the one to delete to change its pointer
Step 3: Change node 2's next pointer to skip node 3 and point to node 4
Head -> 1 -> 2 -> 4 -> 5 -> null
Why: This removes node 3 from the chain
Result:
Head -> 1 -> 2 -> 4 -> 5 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, data):
        new_node = Node(data)
        if not self.head:
            self.head = new_node
            return
        last = self.head
        while last.next:
            last = last.next
        last.next = new_node

    def delete_at_position(self, position):
        if not self.head:
            return  # empty list, nothing to delete
        if position == 1:
            self.head = self.head.next  # remove head
            return
        curr = self.head
        count = 1
        while curr and count < position - 1:
            curr = curr.next
            count += 1
        if not curr or not curr.next:
            return  # position out of range
        curr.next = curr.next.next  # skip the node at position

    def __str__(self):
        result = []
        curr = self.head
        while curr:
            result.append(str(curr.data))
            curr = curr.next
        return ' -> '.join(result) + ' -> null'

# Driver code
ll = LinkedList()
for i in [1, 2, 3, 4, 5]:
    ll.append(i)
print("Before deletion:", ll)
ll.delete_at_position(3)
print("After deletion at position 3:", ll)
if position == 1: self.head = self.head.next # remove head
handle deletion of the first node by moving head pointer
while curr and count < position - 1: curr = curr.next count += 1
advance to node before the one to delete
if not curr or not curr.next: return # position out of range
check if position is valid within list length
curr.next = curr.next.next # skip the node at position
unlink the node at position by skipping it
OutputSuccess
Before deletion: 1 -> 2 -> 3 -> 4 -> 5 -> null After deletion at position 3: 1 -> 2 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because we may need to traverse up to n nodes to reach the position
Space: O(1) because we only use a few pointers and no extra data structures
vs Alternative: Compared to rebuilding the list without the node, this pointer change is more efficient and uses constant space
Edge Cases
empty list
nothing happens, no error
DSA Python
if not self.head:
    return  # empty list, nothing to delete
delete head node (position 1)
head moves to next node
DSA Python
if position == 1:
    self.head = self.head.next  # remove head
position out of range (greater than list length)
no deletion occurs
DSA Python
if not curr or not curr.next:
    return  # position out of range
When to Use This Pattern
When asked to remove a node at a specific position in a linked list, use the delete-at-position pattern by adjusting pointers to skip the target node.
Common Mistakes
Mistake: Trying to delete the node without updating the previous node's next pointer
Fix: Always find the node before the target and update its next pointer to skip the target node
Mistake: Not handling deletion of the head node separately
Fix: Check if position is 1 and update head pointer accordingly
Summary
Deletes a node at a given position by changing pointers to skip it.
Use when you need to remove a node from anywhere in a singly linked list.
The key is to find the node before the target and link it to the node after the target.