0
0
DSA Pythonprogramming

Insert at Beginning of Doubly Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
Add a new item at the start by linking it before the current first item and updating pointers.
Analogy: Imagine adding a new book at the front of a shelf where each book knows the one before and after it.
null ← 1 ↔ 2 ↔ 3 -> null
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, insert value 0 at beginning
Goal: Add 0 at the start so the list becomes 0 ↔ 1 ↔ 2 ↔ 3
Step 1: Create new node with value 0
null ← 1 ↔ 2 ↔ 3 -> null, new_node(0)
Why: We need a new node to insert at the beginning
Step 2: Set new_node.next to current head (1)
new_node(0) -> 1 ↔ 2 ↔ 3 -> null
Why: Link new node forward to old first node
Step 3: Set current head.prev to new_node
new_node(0) ↔ 1 ↔ 2 ↔ 3 -> null
Why: Link old first node back to new node
Step 4: Update head pointer to new_node
null ← 0 ↔ 1 ↔ 2 ↔ 3 -> null ↑head
Why: New node is now the first node in the list
Result:
null ← 0 ↔ 1 ↔ 2 ↔ 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 insert_at_beginning(self, data):
        new_node = Node(data)  # create new node
        if self.head is None:
            self.head = new_node  # empty list, new node is head
            return
        new_node.next = self.head  # link new node forward to old head
        self.head.prev = new_node  # link old head back to new node
        self.head = new_node  # update head to new 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.insert_at_beginning(3)
dll.insert_at_beginning(2)
dll.insert_at_beginning(1)
# List is now 1 ↔ 2 ↔ 3

# Insert 0 at beginning
dll.insert_at_beginning(0)
dll.print_list()
new_node = Node(data) # create new node
create new node to insert
if self.head is None: self.head = new_node # empty list, new node is head return
handle empty list by setting head to new node
new_node.next = self.head # link new node forward to old head
link new node's next to current head
self.head.prev = new_node # link old head back to new node
link old head's prev to new node
self.head = new_node # update head to new node
update head pointer to new node
OutputSuccess
0 ↔ 1 ↔ 2 ↔ 3 -> null
Complexity Analysis
Time: O(1) because insertion at the beginning only changes a few pointers without traversal
Space: O(1) because no extra space is used except the new node
vs Alternative: Compared to inserting at the end which may require traversal O(n), this is faster and simpler
Edge Cases
empty list
new node becomes the head with no next or prev
DSA Python
if self.head is None:
            self.head = new_node  # empty list, new node is head
            return
single element list
new node links to old head, old head links back to new node
DSA Python
new_node.next = self.head  # link new node forward to old head
When to Use This Pattern
When you need to add an item quickly at the start of a list and keep two-way links, use insert at beginning of doubly linked list.
Common Mistakes
Mistake: Forgetting to update the old head's prev pointer to the new node
Fix: Always set old head.prev = new_node after linking new_node.next to old head
Mistake: Not handling empty list case causing errors
Fix: Check if head is None and set head to new node directly
Summary
Adds a new node at the start of a doubly linked list by updating next and prev pointers.
Use when you want to quickly add elements to the front of a list with backward and forward links.
The key is to link the new node before the current head and update the head pointer.