Bird
Raised Fist0
DSA Goprogramming~10 mins

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

Choose your learning style10 modes available
Concept Flow - Delete from Beginning of Doubly Linked List
Start at head node
↓
Check if list is empty?
↓
Move head to head.next
↓
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 Go
func deleteFromBeginning(head *Node) *Node {
    if head == nil {
        return nil
    }
    newHead := head.next
    if newHead != nil {
        newHead.prev = nil
    }
    return newHead
}
Deletes the first node of a doubly linked list and returns the new head.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Start with list10 <-> 20 <-> 30head points to node 10ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:10 │←── │ prev:20 │ │ next:20│──→ │ next:30 │──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if list empty10 <-> 20 <-> 30head != nil, continueSame as step 1
3Move head to head.next20 <-> 30head updated to node 20ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:10│←── │ prev:20 │←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
4Set new head.prev = nil20 <-> 30node 20.prev set to āˆ…ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20 │←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Old head removed20 <-> 30Old node 10 disconnectedā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:30│ │ prev:āˆ… │←── │ prev:20 │←── │ next:30│──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Return new head20 <-> 30head points to node 20Same as step 5
šŸ’” Old head node removed, new head updated, list updated correctly
Variable Tracker
VariableStartAfter Step 3After Step 4Final
headnode 10node 20node 20node 20
head.prevāˆ…10āˆ…āˆ…
head.nextnode 20node 30node 30node 30
oldHeadnode 10node 10node 10removed
Key Moments - 3 Insights
Why do we set new head.prev to null after moving head?
Because the new head is now the first node, it should not point back to the old head. See step 4 in execution_table where new head.prev is set to āˆ….
What happens if the list is empty when we try to delete?
The function checks if head is nil at step 2 and returns nil immediately, so no deletion happens.
Why do we not need to update the old head's next or prev pointers explicitly?
Because after moving head and updating new head.prev, the old head is disconnected by losing references and will be removed by garbage collection. See step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of head after step 3?
Anode 10
Bnode 20
Cnil
Dnode 30
šŸ’” Hint
Check the 'Pointer Changes' column at step 3 in execution_table
At which step does the new head.prev pointer get set to null?
AStep 4
BStep 3
CStep 2
DStep 5
šŸ’” Hint
Look at the 'Operation' and 'Pointer Changes' columns in execution_table
If the list was empty initially, what would the function return?
AThe original head node
BA new node
Cnil
DAn error
šŸ’” Hint
See step 2 where the function checks if head is nil
Concept Snapshot
Delete from Beginning of Doubly Linked List:
- Check if list is empty (head == nil)
- Move head to head.next
- Set new head.prev = nil
- Old head node is disconnected and removed
- Return new head
- Handles empty list safely
Full Transcript
To delete the first node in a doubly linked list, start at the head. If the list is empty, return nil immediately. Otherwise, move the head pointer to the next node. Then set the new head's prev pointer to null to disconnect it from the old head. The old head node is now disconnected and will be removed by garbage collection. Finally, return the new head pointer. This process safely updates the list and handles empty lists without errors.

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 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:

    Head points to new first node after deletion [OK]
Hint: Head always points to the first node [OK]
Common Mistakes:
  • Thinking head becomes null without checking list emptiness
  • Assuming head points to last node after deletion
  • Not updating head pointer after deletion
2. Which of the following Go code snippets correctly deletes the first node from a doubly linked list named dll?
easy
A. dll.head.prev = nil; dll.head = dll.head.next
B. dll.head = dll.head.next; if dll.head != nil { dll.head.prev = nil }
C. dll.head.next.prev = nil; dll.head = dll.head.next
D. dll.head = nil; dll.tail = nil

Solution

  1. Step 1: Move head to next node

    Set dll.head = dll.head.next to remove first node reference.
  2. Step 2: Update new head's prev pointer

    If new head exists, set dll.head.prev = nil to detach backward link.
  3. Final Answer:

    dll.head = dll.head.next; if dll.head != nil { dll.head.prev = nil } -> Option B
  4. Quick Check:

    Correct pointer updates for deletion [OK]
Hint: Update head then clear new head's prev [OK]
Common Mistakes:
  • Setting prev pointer before moving head
  • Not checking if new head is nil
  • Clearing head and tail without condition
3. Given the following doubly linked list with nodes 10 <-> 20 <-> 30, what will be the list after deleting the first node?
type Node struct {
  data int
  prev *Node
  next *Node
}

head := &Node{data: 10}
second := &Node{data: 20}
third := &Node{data: 30}
head.next = second
second.prev = head
second.next = third
third.prev = second

// Delete from beginning
head = head.next
head.prev = nil

// Print list
curr := head
for curr != nil {
  fmt.Print(curr.data)
  if curr.next != nil {
    fmt.Print(" <-> ")
  }
  curr = curr.next
}
medium
A. 20 <-> 30
B. 10 <-> 20 <-> 30
C. 30
D. nil

Solution

  1. Step 1: Initial list setup

    List is 10 <-> 20 <-> 30 with head at 10.
  2. Step 2: Delete first node

    Head moves to 20, and 20's prev is set to nil, detaching 10.
  3. Final Answer:

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

    First node removed, list starts at 20 [OK]
Hint: Head moves to next, prev cleared [OK]
Common Mistakes:
  • Not clearing prev pointer of new head
  • Printing old head node
  • Confusing next and prev pointers
4. Identify the error in this Go code snippet that tries to delete the first node from a doubly linked list:
if dll.head != nil {
  dll.head = dll.head.next
  dll.head.prev = nil
}
medium
A. It should assign dll.head.prev to dll.head.next
B. It should set dll.head.next.prev to nil instead
C. No error, code is correct
D. It does not check if dll.head.next is nil before accessing prev

Solution

  1. Step 1: Check condition before accessing pointers

    The code moves head to next node without checking if next node exists.
  2. Step 2: Accessing prev of nil causes runtime error

    If dll.head.next is nil, then dll.head becomes nil and accessing dll.head.prev causes panic.
  3. Final Answer:

    It does not check if dll.head.next is nil before accessing prev -> Option D
  4. Quick Check:

    Always check for nil before pointer access [OK]
Hint: Check nil before accessing pointers [OK]
Common Mistakes:
  • Assuming next node always exists
  • Not handling empty list after deletion
  • Confusing prev and next pointers
5. You have a doubly linked list with nodes 5 <-> 15. You want to delete the first node and then insert a new node with value 10 at the beginning. Which sequence of operations correctly updates the list?
hard
A. Delete first node by moving head to next; create new node with prev = head, next = nil; set head.next to new node
B. Delete first node by setting head to nil; create new node with next = nil, prev = nil; set head to new node
C. Delete first node by moving head to next and clearing prev; then create new node with next = head, prev = nil; update head.prev to new node; finally set head to new node
D. Delete first node by moving head to next; create new node with next = nil, prev = nil; set head to new node

Solution

  1. Step 1: Delete first node properly

    Move head to next node and set new head's prev to nil to detach old first node.
  2. Step 2: Insert new node at beginning

    Create new node with next pointing to current head and prev nil; update current head's prev to new node; update head pointer to new node.
  3. Final Answer:

    Delete first node by moving head to next and clearing prev; then create new node with next = head, prev = nil; update head.prev to new node; finally set head to new node -> Option C
  4. Quick Check:

    Proper delete then insert at head [OK]
Hint: Delete then insert by updating head and pointers [OK]
Common Mistakes:
  • Not updating prev pointer of old head after insertion
  • Setting new node's prev incorrectly
  • Overwriting head without linking new node
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