0
0
DSA Pythonprogramming

Delete from Beginning of Doubly Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
Removing the first item means moving the start pointer to the next item and fixing links so the list stays connected.
Analogy: Imagine a train with cars linked front and back. Removing the first car means detaching it and making the second car the new front, making sure it has no car before it.
null ← 1 [head↑] -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, delete from beginning
Goal: Remove the first node and update the list so it starts at the second node
Step 1: head moves from node 1 to node 2
null ← 1 -> 2 [head↑] -> 3 -> null
Why: We want to remove the first node, so head must point to the second node
Step 2: set previous pointer of new head (node 2) to null
null ← 2 [head↑] -> 3 -> null
Why: The new first node should not point back to the removed node
Step 3: old first node (1) is detached and removed
null ← 2 [head↑] -> 3 -> null
Why: The list now starts at node 2 with proper links, node 1 is gone
Result:
null ← 2 [head↑] -> 3 -> null
Annotated Code
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

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

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

    def delete_from_beginning(self):
        if not self.head:
            return  # empty list, nothing to delete
        if not self.head.next:
            self.head = None  # only one node, now empty
            return
        self.head = self.head.next  # move head to second node
        self.head.prev = None  # detach old first node

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

# Driver code
dll = DoublyLinkedList()
dll.append(1)
dll.append(2)
dll.append(3)
dll.delete_from_beginning()
dll.print_list()
if not self.head:
check if list is empty, no deletion needed
if not self.head.next:
handle single node list by setting head to None
self.head = self.head.next
move head pointer to second node to remove first
self.head.prev = None
remove backward link to old first node
OutputSuccess
2 -> 3 -> null
Complexity Analysis
Time: O(1) because deletion from beginning only changes a few pointers without traversal
Space: O(1) because no extra space is used, only pointer updates
vs Alternative: Compared to deleting from end which requires traversal O(n), deleting from beginning is faster and simpler
Edge Cases
empty list
no deletion occurs, list remains empty
DSA Python
if not self.head:
single node list
head is set to None, list becomes empty
DSA Python
if not self.head.next:
When to Use This Pattern
When asked to remove the first element of a doubly linked list, use this pattern to update head and fix backward links quickly.
Common Mistakes
Mistake: Not setting the new head's prev pointer to None after deletion
Fix: Always set self.head.prev = None after moving head to next node
Mistake: Not handling empty or single node list cases, causing errors
Fix: Add checks for empty list and single node list before deletion
Summary
Deletes the first node of a doubly linked list by moving head and fixing links.
Use when you need to remove the front element efficiently from a doubly linked list.
The key is to move head forward and remove backward link to the old first node.