Bird
Raised Fist0
DSA Javaprogramming~10 mins

Insert at Beginning of Doubly Linked List in DSA Java - 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
The new node is created and linked before the current head. If the list is not empty, the old head's previous pointer is updated. Finally, head points to the new node.
Execution Sample
DSA Java
class Node {
  int data;
  Node prev, next;
  Node(int d) { data = d; prev = next = null; }
}

// Insert new node at beginning
void insertAtBeginning(int data) {
  Node new_node = new Node(data);
  new_node.next = head;
  if (head != null) {
    head.prev = new_node;
  }
  head = new_node;
}
This code creates a new node and inserts it at the start of a doubly linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=5āˆ…new_node: data=5, prev=āˆ…, next=āˆ…List is empty
2Set new_node.next = head (head is āˆ…)āˆ…new_node.next = āˆ…new_node (5) → āˆ…
3Check if head != null (head is āˆ…)āˆ…No changeNo prev pointer update needed
4Set head = new_node5head → new_node (5)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Insert another node with data=105Create new_node: data=10, prev=āˆ…, next=āˆ…List before insertion: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:5 │ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Set new_node.next = head (node 5)5new_node.next → node 5new_node (10) → node (5)
7Since head != null, set head.prev = new_node5node 5.prev → new_node (10)node (5) prev → new_node (10)
8Set head = new_node (10)10 → 5head → new_node (10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:5 │ │ prev:āˆ… │←── │ prev:10│ │ next:5 │──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
9Insert another node with data=1510 → 5Create new_node: data=15, prev=āˆ…, next=āˆ…List before insertion: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:5 │ │ prev:āˆ… │←── │ prev:10│ │ next:5 │──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
10Set new_node.next = head (node 10)10 → 5new_node.next → node 10new_node (15) → node (10)
11Since head != null, set head.prev = new_node10 → 5node 10.prev → new_node (15)node (10) prev → new_node (15)
12Set head = new_node (15)15 → 10 → 5head → new_node (15)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:15│ │ data:10│ │ data:5 │ │ prev:āˆ… │←── │ prev:15│←── │ prev:10│ │ next:10│──→ │ next:5 │──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
13End of insertions15 → 10 → 5No changeFinal list state
šŸ’” No more insertions, list head points to newest node 15
Variable Tracker
VariableStartAfter Step 1After Step 5After Step 9Final
headāˆ…āˆ…Node(5)Node(10)Node(15)
new_nodeāˆ…Node(5)Node(10)Node(15)Node(15)
list size00123
Key Moments - 3 Insights
Why do we set new_node.next = head before changing head?
Because new_node must point to the old first node before head is updated, as shown in execution_table step 6 and 10.
Why do we update head.prev to new_node only if head is not null?
If head is null, the list is empty and no previous node exists to update. This is shown in step 3 where no update happens.
How does the prev pointer help in a doubly linked list?
It allows backward traversal and proper linking when inserting at the beginning, as seen in steps 7 and 11 where head.prev is updated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what does the head pointer reference?
ANode with data 5
BNull
CNode with data 10
DNode with data 15
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 8.
At which step does the list first have two nodes linked?
AStep 4
BStep 8
CStep 6
DStep 12
šŸ’” Hint
Look for when head points to new_node and new_node.next points to old head.
If the list was initially empty, which step shows the first node insertion?
AStep 4
BStep 1
CStep 5
DStep 9
šŸ’” Hint
Check when head changes from null to a node for the first time.
Concept Snapshot
Insert at Beginning of Doubly Linked List:
- Create new node
- Set new_node.next = head
- If head not null, set head.prev = new_node
- Update head = new_node
- List grows at front, prev and next pointers updated accordingly
Full Transcript
To insert a node at the beginning of a doubly linked list, first create the new node. Then set its next pointer to the current head. If the list is not empty, update the old head's prev pointer to the new node. Finally, update the head pointer to the new node. This process ensures the list is correctly linked in both directions. The execution table shows step-by-step how nodes and pointers change, starting from an empty list and adding nodes with data 5, 10, and 15 at the beginning. The variable tracker follows the head pointer and list size changes. Key moments clarify why pointer updates happen in a specific order. The visual quiz tests understanding of pointer references and list state at different steps.

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 after the tail node
B. To reverse the entire list
C. To delete the first node of the list
D. To add a new node before the current head node

Solution

  1. Step 1: Understand the position of insertion

    Inserting at the beginning means placing the new node before the current head node.
  2. Step 2: Identify the effect on the list

    The new node becomes the new head, and its next pointer points to the old head.
  3. Final Answer:

    To add a new node before the current head node -> Option D
  4. Quick Check:

    Insert at beginning = add before head [OK]
Hint: Beginning insertion means new node becomes the head [OK]
Common Mistakes:
  • Confusing beginning with end insertion
  • Thinking insertion deletes nodes
  • Mixing up singly and doubly linked list behavior
2. Which of the following is the correct syntax to create a new node in Java for a doubly linked list?
easy
A. Node newNode = Node(data); newNode.prev = null; newNode.next = null;
B. Node newNode = new Node(data); newNode.prev = null; newNode.next = null;
C. Node newNode = new Node(); newNode.data = data; newNode.prev = null; newNode.next = null;
D. Node newNode = new Node(data); newNode.prev = data; newNode.next = data;

Solution

  1. Step 1: Understand Java object creation syntax

    In Java, objects are created using the 'new' keyword followed by the constructor.
  2. Step 2: Check node initialization

    The node's previous and next pointers should be set to null initially.
  3. Final Answer:

    Node newNode = new Node(data); newNode.prev = null; newNode.next = null; -> Option B
  4. Quick Check:

    Java new object = new Constructor() [OK]
Hint: Use 'new' keyword and constructor to create objects [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Assigning data to prev or next pointers
  • Using incorrect constructor syntax
3. What will be the output of the doubly linked list after inserting 10 at the beginning of the list containing nodes 20 <-> 30?
medium
A. 10 <-> 20 <-> 30
B. 20 <-> 30 <-> 10
C. 30 <-> 20 <-> 10
D. 10 <-> 30 <-> 20

Solution

  1. Step 1: Identify current list order

    The list currently is 20 <-> 30, with 20 as head and 30 as tail.
  2. Step 2: Insert 10 at beginning

    New node 10 becomes head, its next points to 20, and 20's prev points to 10.
  3. Final Answer:

    10 <-> 20 <-> 30 -> Option A
  4. Quick Check:

    Insert at beginning adds node before head [OK]
Hint: New node at start shifts old head right [OK]
Common Mistakes:
  • Appending instead of prepending
  • Not updating previous pointer of old head
  • Reversing node order incorrectly
4. Given the following code snippet for inserting at the beginning of a doubly linked list, what is the error?
void insertAtBeginning(int data) {
  Node newNode = new Node(data);
  newNode.next = head;
  head.prev = newNode;
  head = newNode;
}
medium
A. No error, code works correctly
B. head should be updated before setting newNode.next
C. head.prev = newNode; causes NullPointerException if list is empty
D. newNode.next should be null

Solution

  1. Step 1: Analyze code for empty list case

    If the list is empty, head is null, so head.prev causes NullPointerException.
  2. Step 2: Identify fix

    Check if head is not null before setting head.prev = newNode.
  3. Final Answer:

    head.prev = newNode; causes NullPointerException if list is empty -> Option C
  4. Quick Check:

    Null head means head.prev causes error [OK]
Hint: Check for null head before accessing head.prev [OK]
Common Mistakes:
  • Ignoring empty list case
  • Assuming head is never null
  • Misordering pointer updates
5. You have a doubly linked list with nodes: 5 <-> 15 <-> 25. You want to insert a new node with value 1 at the beginning and then reverse the list. What will be the final list after these operations?
hard
A. 25 <-> 15 <-> 5 <-> 1
B. 1 <-> 5 <-> 15 <-> 25
C. 1 <-> 25 <-> 15 <-> 5
D. 5 <-> 15 <-> 25 <-> 1

Solution

  1. Step 1: Insert 1 at beginning

    List becomes 1 <-> 5 <-> 15 <-> 25 after insertion.
  2. Step 2: Reverse the list

    Reversing swaps head and tail, so list becomes 25 <-> 15 <-> 5 <-> 1.
  3. Final Answer:

    25 <-> 15 <-> 5 <-> 1 -> Option A
  4. Quick Check:

    Insert at start then reverse flips order [OK]
Hint: Insert first, then reverse flips entire list [OK]
Common Mistakes:
  • Reversing before insertion
  • Not updating prev pointers when reversing
  • Confusing node order after reverse
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