0
0
DSA Pythonprogramming

Delete Node at Beginning in DSA Python

Choose your learning style9 modes available
Mental Model
To remove the first item, just move the start pointer to the next item, skipping the first.
Analogy: Imagine a line of people holding hands. To remove the first person, the second person becomes the new start of the line.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, delete node at beginning
Goal: Remove the first node and update the list to start from the second node
Step 1: Set head to point to the second node
head -> [2] -> 3 -> null
Why: We skip the first node by moving head to the next node
Step 2: The first node (1) is no longer referenced and is removed
head -> 2 -> 3 -> null
Why: Without any pointer to node 1, it is effectively deleted
Result:
head -> 2 -> 3 -> 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_beginning(self):
        if not self.head:
            return  # List is empty, nothing to delete
        self.head = self.head.next  # Move head to next node, removing first

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

# Driver code
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
print("Before deletion:", ll)
ll.delete_at_beginning()
print("After deletion:", ll)
if not self.head:
check if list is empty to avoid errors
self.head = self.head.next
advance head pointer to next node to remove first node
OutputSuccess
Before deletion: 1 -> 2 -> 3 -> null After deletion: 2 -> 3 -> null
Complexity Analysis
Time: O(1) because we only change the head pointer once
Space: O(1) because no extra space is used
vs Alternative: Compared to deleting a node in the middle which requires traversal O(n), deleting at beginning is faster and simpler
Edge Cases
empty list
No deletion occurs, list remains empty
DSA Python
if not self.head:
single node list
After deletion, list becomes empty (head is None)
DSA Python
self.head = self.head.next
When to Use This Pattern
When asked to remove the first element of a linked list quickly, use the delete at beginning pattern by moving the head pointer.
Common Mistakes
Mistake: Trying to delete the node by traversing or searching instead of just moving head
Fix: Directly assign head to head.next without traversal
Summary
Deletes the first node by moving the head pointer to the next node.
Use when you need to remove the first element of a linked list efficiently.
The key insight is that the head pointer controls the start of the list, so changing it removes the first node.