📖 Scenario: You are managing a playlist of songs where you want to add new songs to the start of the list so they play first.
🎯 Goal: Build a doubly linked list and write code to insert a new node at the beginning of the list.
📋 What You'll Learn
Define a doubly linked list node struct called Node with data, prev, and next fields
Create a variable called head to point to the start of the list
Write a function called insertAtBeginning that takes a pointer to head and an integer data to insert a new node at the beginning
Print the list from head to tail after insertion
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and undo-redo features where you need to move forward and backward easily.
💼 Career
Understanding linked lists is fundamental for software developers working with data structures, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create the doubly linked list node and initial list
Define a struct called Node with data int, prev *Node, and next *Node. Then create a variable called head that points to a node with data = 10 and both prev and next as nil.
DSA Go
Hint
Define the struct with the three fields. Then create head as a pointer to a new Node with data 10 and nil pointers.
2
Add a helper variable for new data to insert
Create a variable called newData and set it to 5. This will be the data for the new node to insert at the beginning.
DSA Go
Hint
Just create a variable newData and assign it the value 5.
3
Write the function to insert at the beginning
Write a function called insertAtBeginning that takes a pointer to a pointer to Node called head and an integer data. Inside, create a new node with data, set its next to the current *head, update the current head's prev to the new node if head is not nil, then update *head to the new node.
DSA Go
Hint
Create the new node with data and next pointing to current head. If head is not nil, update its prev to new node. Finally, update head to new node.
4
Print the doubly linked list from head to tail
Use a for loop with a variable current starting at head. Print each node's data followed by " -> " until current is nil. After the loop, print "nil".
DSA Go
Hint
Start from head and move to next nodes, printing data and arrows until you reach nil.
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 before the current head node
B. To add a new node after the tail node
C. To delete the first node of the list
D. To reverse the entire list
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 A
Quick Check:
Insert at beginning = add before head [OK]
Hint: Beginning means before the head node [OK]
Common Mistakes:
Confusing beginning with end insertion
Thinking insertion deletes nodes
Assuming insertion reverses list
2. Which of the following Go code snippets correctly updates the previous pointer of the old head node when inserting a new node at the beginning?
easy
A. newNode.prev = oldHead
B. oldHead.prev = newNode
C. newNode.next = oldHead.prev
D. oldHead.next = newNode
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 B
Quick Check:
Old head prev points to new node [OK]
Hint: Old head's prev points to new node [OK]
Common Mistakes:
Assigning newNode.prev incorrectly
Mixing next and prev pointers
Updating oldHead.next instead of prev
3. Consider this Go code inserting nodes at the beginning of a doubly linked list:
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?
medium
A. 20 -> 30 -> 10 -> null
B. 10 -> 20 -> 30 -> null
C. null
D. 30 -> 20 -> 10 -> null
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 D
Quick Check:
Last inserted node is new head [OK]
Hint: Last inserted node becomes new head [OK]
Common Mistakes:
Assuming insertion adds at end
Confusing order of nodes
Ignoring pointer updates
4. The following Go function attempts to insert a node at the beginning of a doubly linked list but causes a runtime panic:
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?
medium
A. head is nil and head.prev causes nil pointer dereference
B. newNode.next is not assigned correctly
C. head = newNode does not update original head pointer
D. data field is not initialized
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 A
Quick Check:
Accessing prev on nil head causes panic [OK]
Hint: Check for nil before accessing head.prev [OK]
Common Mistakes:
Ignoring nil check before pointer access
Thinking assignment head = newNode updates caller
Assuming data field causes panic
5. You have a doubly linked list with nodes: 5 <-> 10 <-> 15. You want to insert a new node with value 3 at the beginning. After insertion, what will be the correct sequence of nodes when traversed from head to tail?
hard
A. 15 -> 10 -> 5 -> 3 -> null
B. 5 -> 10 -> 15 -> 3 -> null
C. 3 -> 5 -> 10 -> 15 -> null
D. 3 -> 15 -> 10 -> 5 -> null
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 C
Quick Check:
New node becomes new head [OK]
Hint: New node becomes new head, old head shifts right [OK]
Common Mistakes:
Inserting at end instead of beginning
Reversing node order incorrectly
Not updating head pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.