Bird
Raised Fist0
DSA Javaprogramming~10 mins

Delete from Beginning of Doubly Linked List in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head
↓
Check if head is null?
Yes→List empty, nothing to delete
No↓
Move head to head.next
↓
If new head not null, set new head.prev = null
↓
Old head node removed
↓
Done
Start at the head node, check if list is empty. If not, move head to next node and update pointers to remove old head.
Execution Sample
DSA Java
class Node {
  int data;
  Node prev, next;
}

// Delete from beginning
if (head != null) {
  Node oldHead = head;
  head = head.next;
  if (head != null) head.prev = null;
  oldHead.prev = null;
  oldHead.next = null;
}
Deletes the first node of a doubly linked list by moving head and updating pointers.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Initial list10 <-> 20 <-> 30head → node 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10│←── │ prev:20│ │ next:20│──→ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Move head to head.next20 <-> 30head: 10 → 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head (old head 10 still points prev:āˆ… next:20)
3Set new head.prev = null20 <-> 30head.prev: 10 → āˆ…ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Old head (10) disconnected20 <-> 30old head prev and next clearedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head Old node 10: ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ (removed)
5End20 <-> 30No changesā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20│ │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
šŸ’” Head moved to next node; old head disconnected and removed.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
headNode 10Node 20Node 20Node 20
head.prevnullNode 10nullnull
old head (node 10) prevnullnullnullnull
old head (node 10) nextNode 20Node 20Node 20null
Key Moments - 3 Insights
Why do we set head.prev to null after moving head?
Because the new head is the first node, it should not point back to any node. See execution_table step 3 where head.prev changes from node 10 to null.
What happens if the list is empty before deletion?
If head is null, no deletion happens. This is shown in concept_flow where the check for null head leads to no operation.
Why do we disconnect old head's next and prev pointers?
To fully remove the old head from the list and avoid accidental references. See execution_table step 4 where old head pointers are cleared.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of head.prev?
Anull
BNode 10
CNode 20
DNode 30
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3 in execution_table.
At which step does the head pointer move from node 10 to node 20?
AStep 1
BStep 2
CStep 3
DStep 4
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table.
If the list had only one node, what would be the head after deletion?
ANode next to head
BThe same node
Cnull
DNode prev to head
šŸ’” Hint
Refer to concept_flow and think about what happens when head.next is null.
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Move head pointer to head.next
- If new head exists, set head.prev = null
- Old head disconnected to remove it
- Handles empty list by checking if head is null
- Result: list starts from second node or becomes empty
Full Transcript
To delete the first node in a doubly linked list, start at the head. If the list is empty (head is null), do nothing. Otherwise, move the head pointer to the next node. Then, if the new head is not null, set its prev pointer to null to disconnect it from the old head. Finally, clear the old head's pointers to fully remove it. This process updates the list so it starts from the second node or becomes empty if there was only one node.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node from a doubly linked list?
easy
A. It moves to the second node in the list
B. It becomes null immediately
C. It points to the last node
D. It remains unchanged

Solution

  1. Step 1: Understand the head pointer role

    The head pointer always points to the first node in the doubly linked list.
  2. Step 2: Effect of deleting the first node

    When the first node is deleted, the head pointer must move to the next node, which becomes the new first node.
  3. Final Answer:

    It moves to the second node in the list -> Option A
  4. Quick Check:

    Deleting first node updates head to next node [OK]
Hint: Head always points to first node; update it after deletion [OK]
Common Mistakes:
  • Not updating head pointer after deletion
  • Setting head to null even if list has more nodes
  • Confusing head with tail pointer
2. Which of the following is the correct Java statement to update the prev pointer of the new head node after deleting the first node in a doubly linked list?
easy
A. head = null.prev;
B. head.next = null;
C. head.prev = null;
D. head.prev.next = null;

Solution

  1. Step 1: Identify the new head node

    After deleting the first node, the new head is the second node in the list.
  2. Step 2: Set the previous pointer of new head to null

    Since the new head is the first node now, its prev pointer must be null to indicate no node before it.
  3. Final Answer:

    head.prev = null; -> Option C
  4. Quick Check:

    New head prev pointer must be null [OK]
Hint: New head's prev pointer is always null [OK]
Common Mistakes:
  • Setting head.next to null instead of head.prev
  • Using invalid syntax like null.prev
  • Trying to access prev.next which may cause error
3. Consider the following Java code snippet for deleting the first node from a doubly linked list:
if (head == null) return;
head = head.next;
if (head != null) head.prev = null;

What will be the state of the list after deleting the first node from the list 1 <-> 2 <-> 3?
medium
A. 1 <-> 3
B. Empty list
C. 3 only
D. 2 <-> 3

Solution

  1. Step 1: Initial list and deletion

    The list is 1 <-> 2 <-> 3. Deleting the first node means removing node with value 1.
  2. Step 2: Update head and prev pointer

    Head moves to node 2. Then head.prev is set to null, so node 2 has no previous node.
  3. Final Answer:

    2 <-> 3 -> Option D
  4. Quick Check:

    First node removed, head points to second node [OK]
Hint: After deletion, head points to second node, prev set null [OK]
Common Mistakes:
  • Not setting head.prev to null causing broken links
  • Assuming node 1 still exists
  • Confusing next and prev pointers
4. The following code attempts to delete the first node from a doubly linked list but causes a runtime error:
if (head == null) return;
head = head.next;
head.prev = null;

What is the error and how to fix it?
medium
A. Syntax error due to missing semicolon; add semicolon after head.prev = null
B. NullPointerException when list has one node; fix by checking if head is null before setting head.prev
C. Logical error: head should not be updated; remove head = head.next
D. No error; code works fine

Solution

  1. Step 1: Identify cause of NullPointerException

    If the list has only one node, after head = head.next, head becomes null. Then head.prev causes NullPointerException.
  2. Step 2: Fix by checking head before accessing prev

    Add a condition to check if head is not null before setting head.prev = null.
  3. Final Answer:

    NullPointerException when list has one node; fix by checking if head is null before setting head.prev -> Option B
  4. Quick Check:

    Always check for null before accessing object members [OK]
Hint: Check head != null before head.prev = null to avoid errors [OK]
Common Mistakes:
  • Not checking for null causing runtime errors
  • Assuming list always has multiple nodes
  • Ignoring exception details
5. You have a doubly linked list with nodes 10 <-> 20 <-> 30 <-> 40. After deleting the first node twice using the correct deletion method, what will be the printed list?
hard
A. 30 <-> 40
B. 20 <-> 30 <-> 40
C. 40 only
D. Empty list

Solution

  1. Step 1: First deletion

    Deleting first node removes 10. Head moves to 20, head.prev set to null. List becomes 20 <-> 30 <-> 40.
  2. Step 2: Second deletion

    Deleting first node again removes 20. Head moves to 30, head.prev set to null. List becomes 30 <-> 40.
  3. Final Answer:

    30 <-> 40 -> Option A
  4. Quick Check:

    Two deletions remove first two nodes [OK]
Hint: Each deletion moves head forward and updates prev to null [OK]
Common Mistakes:
  • Deleting only once instead of twice
  • Not updating head.prev after second deletion
  • Printing old list without changes
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