Bird
Raised Fist0
DSA Goprogramming

Delete from Beginning of Doubly Linked List in DSA Go

Choose your learning style10 modes available
Mental Model
To remove the first item, we move the start pointer to the next item and fix links so the list stays connected.
Analogy: Imagine a train where the first carriage is detached and the second carriage becomes the new front, with no loose ends.
null ← 1 ↔ 2 ↔ 3 -> null
↑head
Dry Run Walkthrough
Input: list: 1 ↔ 2 ↔ 3, delete from beginning
Goal: Remove the first node and update the list to start from the second node
Step 1: head moves from node 1 to node 2
null ← 1 ↔ 2 ↔ 3 -> null
     ↑head
Why: We need to point head to the second node to remove the first
Step 2: set previous pointer of new head (node 2) to null
null ← 2 ↔ 3 -> null
↑head
Why: Disconnect the old first node by removing backward link
Step 3: old first node (1) is now detached and ready for garbage collection
null ← 2 ↔ 3 -> null
↑head
Why: The list is updated and the first node is removed
Result:
null ← 2 ↔ 3 -> null
↑head
Annotated Code
DSA Go
package main

import "fmt"

type Node struct {
	data int
	prev *Node
	next *Node
}

type DoublyLinkedList struct {
	head *Node
}

func (dll *DoublyLinkedList) deleteFromBeginning() {
	if dll.head == nil {
		return // list empty, nothing to delete
	}
	// move head to next node
	dll.head = dll.head.next
	// if new head exists, set its prev to nil
	if dll.head != nil {
		dll.head.prev = nil
	}
}

func (dll *DoublyLinkedList) printList() {
	curr := dll.head
	for curr != nil {
		fmt.Printf("%d", curr.data)
		if curr.next != nil {
			fmt.Printf(" ↔ ")
		}
		curr = curr.next
	}
	fmt.Println()
}

func main() {
	dll := DoublyLinkedList{}
	dll.head = &Node{data: 1}
	dll.head.next = &Node{data: 2, prev: dll.head}
	dll.head.next.next = &Node{data: 3, prev: dll.head.next}

	fmt.Print("Original list: ")
	dll.printList()

	dll.deleteFromBeginning()

	fmt.Print("After deleting from beginning: ")
	dll.printList()
}
if dll.head == nil { return }
handle empty list by returning early
dll.head = dll.head.next
advance head pointer to second node to remove first
if dll.head != nil { dll.head.prev = nil }
disconnect backward link of new head to detach old first node
OutputSuccess
Original list: 1 ↔ 2 ↔ 3 After deleting from beginning: 2 ↔ 3
Complexity Analysis
Time: O(1) because we only change a few pointers at the start
Space: O(1) because no extra memory is used besides pointers
vs Alternative: Compared to deleting from the end which requires traversal O(n), deleting from beginning is faster and simpler
Edge Cases
empty list
function returns immediately without error
DSA Go
if dll.head == nil { return }
single element list
head becomes nil, list becomes empty
DSA Go
dll.head = dll.head.next
When to Use This Pattern
When you need to remove the first item quickly from a doubly linked list, use delete from beginning to update head and fix links.
Common Mistakes
Mistake: Not setting the new head's prev pointer to nil after deletion
Fix: Always set new head's prev to nil to avoid backward link to removed node
Mistake: Not handling empty list before deletion
Fix: Check if head is nil before deleting to avoid errors
Summary
Deletes the first node of a doubly linked list by moving head and fixing pointers.
Use when you want to remove the front element efficiently.
The key is to move head forward and disconnect the backward link of the new head.

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