What if you could add new things to the front instantly without moving everything else?
Why Insert at Beginning of Doubly Linked List in DSA Go?
Imagine you have a list of tasks written on sticky notes lined up on your desk. You want to add a new urgent task right at the front so you see it first.
If you try to do this by moving each sticky note one by one to make space, it will take a lot of time and effort.
Manually shifting all tasks to add one at the front is slow and tiring. You might drop or lose some notes, or mix up the order.
This is like manually moving every item in a list to insert at the start, which wastes time and can cause mistakes.
A doubly linked list lets you add a new item at the beginning quickly by just changing a few pointers. You don't need to move all other items.
This makes adding at the front fast and safe, like slipping a new sticky note right at the front without disturbing others.
for i := len(tasks) - 1; i >= 0; i-- { tasks[i+1] = tasks[i] } tasks[0] = newTask
newNode.next = head
if head != nil {
head.prev = newNode
}
head = newNodeYou can quickly add important items at the front without disturbing the rest, making your list flexible and efficient.
In a music playlist app, when you add a new song to play next, it goes right at the start without rearranging the whole list.
Manually inserting at the start is slow and error-prone.
Doubly linked lists let you insert at the beginning by changing just a few links.
This makes adding at the front fast, safe, and efficient.
Practice
Solution
Step 1: Understand the head node position
The head node is the first node in the doubly linked list.Step 2: Insert new node before head
Inserting at the beginning means placing the new node before the current head and updating pointers.Final Answer:
To add a new node before the current head node -> Option AQuick Check:
Insert at beginning = add before head [OK]
- Confusing beginning with end insertion
- Thinking insertion deletes nodes
- Assuming insertion reverses list
Solution
Step 1: Identify pointer to update
When inserting at beginning, old head's previous pointer must point to new node.Step 2: Correct assignment
Setting oldHead.prev = newNode correctly links old head back to new node.Final Answer:
oldHead.prev = newNode -> Option BQuick Check:
Old head prev points to new node [OK]
- Assigning newNode.prev incorrectly
- Mixing next and prev pointers
- Updating oldHead.next instead of prev
type Node struct {
data int
prev *Node
next *Node
}
func insertAtBeginning(head **Node, data int) {
newNode := &Node{data: data}
newNode.next = *head
if *head != nil {
(*head).prev = newNode
}
*head = newNode
}
func main() {
var head *Node
insertAtBeginning(&head, 10)
insertAtBeginning(&head, 20)
insertAtBeginning(&head, 30)
// Print list from head
curr := head
for curr != nil {
fmt.Print(curr.data, " -> ")
curr = curr.next
}
fmt.Print("null")
}
What is the printed output?Solution
Step 1: Trace insertions order
Nodes inserted in order: 10, then 20 at beginning, then 30 at beginning.Step 2: List after insertions
After all insertions, list is 30 (head) -> 20 -> 10 -> null.Final Answer:
30 -> 20 -> 10 -> null -> Option DQuick Check:
Last inserted node is new head [OK]
- Assuming insertion adds at end
- Confusing order of nodes
- Ignoring pointer updates
func insertAtBeginning(head *Node, data int) {
newNode := &Node{data: data}
newNode.next = head
head.prev = newNode
head = newNode
}
What is the main error causing the panic?Solution
Step 1: Check nil pointer usage
If head is nil, accessing head.prev causes runtime panic (nil pointer dereference).Step 2: Identify cause of panic
Function does not check if head is nil before head.prev = newNode, causing panic.Final Answer:
head is nil and head.prev causes nil pointer dereference -> Option AQuick Check:
Accessing prev on nil head causes panic [OK]
- Ignoring nil check before pointer access
- Thinking assignment head = newNode updates caller
- Assuming data field causes panic
Solution
Step 1: Understand insertion at beginning
New node with value 3 is added before current head (5).Step 2: Update pointers and traverse
After insertion, list is 3 (head) -> 5 -> 10 -> 15 -> null.Final Answer:
3 -> 5 -> 10 -> 15 -> null -> Option CQuick Check:
New node becomes new head [OK]
- Inserting at end instead of beginning
- Reversing node order incorrectly
- Not updating head pointer
Ready to prove it?
Pass the test and this pattern moves unknown
