Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Insert at Beginning of Doubly Linked List in DSA Python - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning of Doubly Linked List
Create new node
↓
Set new_node.next = head
↓
If head != null
↓
Set head.prev = new_node
↓
Set head = new_node
↓
Done
Insert a new node at the start by linking it before the current head and updating pointers.
Execution Sample
DSA Python
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

def insert_at_beginning(head, data):
    new_node = Node(data)
    new_node.next = head
    if head:
        head.prev = new_node
    head = new_node
    return head
This code inserts a new node with given data at the beginning of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list: 10 <-> 20 <-> 3010, 20, 30head → Node(10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Create new node with data=55 (new), 10, 20, 30new_node created with data=5new_node: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
3Set new_node.next = head (Node 10)5, 10, 20, 30new_node.next → Node(10)new_node: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:10│──→ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ new_node head
4Since head != null, set head.prev = new_node5, 10, 20, 30Node(10).prev → new_nodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:5 │←── │ prev:10│←── │ prev:20│ │ next:10│──→ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ new_node head
5Set head = new_node5, 10, 20, 30head → new_nodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:5 │←── │ prev:10│←── │ prev:20│ │ next:10│──→ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Return new head5, 10, 20, 30head points to new_nodeFinal list: 5 <-> 10 <-> 20 <-> 30
šŸ’” Insertion complete, new node is now the head of the list.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
headNode(10)Node(10)Node(10)Node(10)Node(5)Node(5)
new_nodeNoneNode(5)Node(5)Node(5)Node(5)Node(5)
new_node.nextNoneNoneNode(10)Node(10)Node(10)Node(10)
head.prevNoneNoneNoneNode(5)NoneNone
Key Moments - 3 Insights
Why do we set new_node.next = head before changing head?
Because we need to link the new node to the current first node before updating head, as shown in step 3 of the execution_table.
Why do we check if head is not null before setting head.prev?
If the list is empty (head is null), there is no node to update prev pointer for. This is shown in step 4 where we only update head.prev if head exists.
What happens to the old head's prev pointer after insertion?
It points back to the new node, maintaining the doubly linked structure, as seen in step 4 where Node(10).prev is set to new_node.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does new_node.next point to?
ANull
BNode with data 5
CNode with data 10
DNode with data 20
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3.
At which step does the head pointer change to the new node?
AStep 2
BStep 5
CStep 4
DStep 6
šŸ’” Hint
Look at the 'Pointer Changes' column to see when head points to new_node.
If the original list was empty (head = null), which step would be skipped?
AStep 4 (set head.prev = new_node)
BStep 5 (set head = new_node)
CStep 3 (set new_node.next = head)
DStep 6 (return head)
šŸ’” Hint
Refer to the condition check before setting head.prev in the code and execution_table.
Concept Snapshot
Insert at Beginning of Doubly Linked List:
- Create new node
- Set new_node.next to current head
- If head exists, set head.prev to new_node
- Update head to new_node
- New node becomes the first element
- Maintains prev and next pointers correctly
Full Transcript
To insert a node at the beginning of a doubly linked list, first create a new node with the given data. Then set the new node's next pointer to the current head of the list. If the list is not empty, update the current head's prev pointer to point back to the new node. Finally, update the head pointer to the new node. This process ensures the new node is the first in the list and the doubly linked structure is maintained with correct prev and next pointers.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node as the first element of the list
B. To remove the last node from the list
C. To reverse the entire list
D. To find the middle node of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means adding a new node before the current first node.
  2. Step 2: Identify the purpose

    This operation makes the new node the first element in the doubly linked list.
  3. Final Answer:

    To add a new node as the first element of the list -> Option A
  4. Quick Check:

    Insert at beginning = Add new first node [OK]
Hint: Insert at beginning means new node becomes the first [OK]
Common Mistakes:
  • Confusing insertion at beginning with deletion
  • Thinking it reverses the list
  • Assuming it finds a node instead of adding
2. Which of the following is the correct syntax to create a new node and insert it at the beginning of a doubly linked list in Python?
easy
A. new_node = Node(data); new_node.prev = head; head.next = new_node; head = new_node
B. new_node = Node(data); head.next = new_node; new_node.prev = head; head = new_node
C. new_node = Node(data); new_node.next = head; head.prev = new_node; head = new_node
D. new_node = Node(data); head.prev = new_node; new_node.next = head.next; head = new_node

Solution

  1. Step 1: Create new node and link next pointer

    new_node.next should point to current head to place it before head.
  2. Step 2: Update previous head's prev pointer and head reference

    head.prev must point back to new_node, then update head to new_node.
  3. Final Answer:

    new_node = Node(data); new_node.next = head; head.prev = new_node; head = new_node -> Option C
  4. Quick Check:

    New node next = head, head prev = new node [OK]
Hint: New node next points to head; head prev points back [OK]
Common Mistakes:
  • Swapping prev and next pointers
  • Not updating head to new node
  • Forgetting to update old head's prev pointer
3. What will be the output of the following code after inserting 10 at the beginning of the doubly linked list?
class Node:
    def __init__(self, data):
        self.data = data
        self.prev = None
        self.next = None

head = Node(20)
new_node = Node(10)
new_node.next = head
head.prev = new_node
head = new_node

# Print list forward
current = head
result = []
while current:
    result.append(current.data)
    current = current.next
print(result)
medium
A. [20, 10]
B. [10, 20]
C. [10]
D. [20]

Solution

  1. Step 1: Insert 10 before 20

    New node with data 10 points next to old head (20), old head prev points back to new node.
  2. Step 2: Traverse from new head and collect data

    Starting at head (10), next is 20, then None. So list is [10, 20].
  3. Final Answer:

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

    List after insertion = [10, 20] [OK]
Hint: Traverse from head to next to see order [OK]
Common Mistakes:
  • Confusing order and printing reversed list
  • Not updating head pointer
  • Printing only one node
4. Identify the error in the following code snippet that inserts a node at the beginning of a doubly linked list:
new_node = Node(5)
new_node.next = head
head = new_node
head.prev = new_node
medium
A. head.prev should be set before updating head
B. new_node.next should be None
C. head.prev should point to None, not new_node
D. No error, code is correct

Solution

  1. Step 1: Analyze pointer assignments

    new_node.next points to old head (correct), but head = new_node happens before head.prev = new_node.
  2. Step 2: Identify the error

    By the time head.prev = new_node executes, head is already the new_node, creating a self-loop. The old head's prev is never set. head.prev must be set before updating head to link the old head back correctly.
  3. Final Answer:

    head.prev should be set before updating head -> Option A
  4. Quick Check:

    Update old head.prev before head = new_node [OK]
Hint: Set head.prev before head = new_node [OK]
Common Mistakes:
  • Setting prev pointer after head update incorrectly
  • Making first node's prev point to itself
  • Ignoring prev pointer for head node
5. Given a doubly linked list with nodes 30 <-> 40 <-> 50, write the sequence of node values after inserting 20 at the beginning and then 10 at the beginning. What is the final list?
hard
A. 10 <-> 30 <-> 20 <-> 40 <-> 50
B. 20 <-> 10 <-> 30 <-> 40 <-> 50
C. 30 <-> 40 <-> 50 <-> 20 <-> 10
D. 10 <-> 20 <-> 30 <-> 40 <-> 50

Solution

  1. Step 1: Insert 20 at beginning

    New node 20 points next to 30, 30 prev points to 20. List becomes 20 <-> 30 <-> 40 <-> 50.
  2. Step 2: Insert 10 at beginning

    New node 10 points next to 20, 20 prev points to 10. List becomes 10 <-> 20 <-> 30 <-> 40 <-> 50.
  3. Final Answer:

    10 <-> 20 <-> 30 <-> 40 <-> 50 -> Option D
  4. Quick Check:

    Insertions add nodes at front in order [OK]
Hint: Insertions at beginning stack nodes in reverse order [OK]
Common Mistakes:
  • Inserting nodes in wrong order
  • Appending instead of prepending
  • Not updating prev pointers correctly
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