📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song is a node with a title. You want to remove the last song from the playlist.
🎯 Goal: Build a doubly linked list with three songs, then delete the last song from the list, and finally print the updated playlist from start to end.
📋 What You'll Learn
Create a doubly linked list with exactly three nodes containing the song titles: "Song1", "Song2", and "Song3"
Create a variable called head pointing to the first node
Write a function called deleteFromEnd that deletes the last node from the doubly linked list
Print the playlist from the head node after deletion in the format: Song1 -> Song2 -> null
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers, and other apps to move forward and backward through items efficiently.
💼 Career
Understanding linked list operations is fundamental for software developers working with data structures, memory management, and system design.
Progress0 / 4 steps
1
Create the doubly linked list with three songs
Create a struct type called Node with fields title string, prev *Node, and next *Node. Then create three nodes with titles "Song1", "Song2", and "Song3". Link them to form a doubly linked list. Finally, create a variable called head pointing to the first node.
DSA Go
Hint
Start by defining the Node struct with the required fields. Then create three nodes and link their prev and next pointers properly. Finally, assign head to the first node.
2
Create a function to delete the last node
Create a function called deleteFromEnd that takes a pointer to the head node head *Node and returns the updated head pointer *Node. This function should delete the last node from the doubly linked list.
DSA Go
Hint
Check if the list is empty or has only one node. If only one node, return nil. Otherwise, move to the last node and remove it by updating the previous node's next pointer to nil. Return the head pointer.
3
Call the delete function to remove the last song
In the main function, call deleteFromEnd with the variable head and update head with the returned value.
DSA Go
Hint
Simply assign head to the result of calling deleteFromEnd(head).
4
Print the updated playlist from head to end
Write a for loop starting with a variable current set to head. In the loop, print the title of each node followed by " -> ". Stop when current is nil, then print "null".
DSA Go
Hint
Use a for loop to traverse from head to the end. Print each node's title followed by " -> ". After the loop, print "null".
Practice
(1/5)
1. What happens to the tail pointer when you delete the last node from a doubly linked list?
easy
A. The tail pointer becomes null immediately.
B. The tail pointer moves to the next node.
C. The tail pointer does not change.
D. The tail pointer moves to the previous node.
Solution
Step 1: Understand the tail pointer role
The tail pointer points to the last node in the doubly linked list.
Step 2: Effect of deleting the last node
When the last node is deleted, the tail pointer must move to the previous node to remain valid.
Final Answer:
The tail pointer moves to the previous node. -> Option D
Quick Check:
Tail moves backward after deletion [OK]
Hint: Tail always points to last node; update to previous on delete [OK]
Common Mistakes:
Not updating tail pointer after deletion
Setting tail to next node which is null
Leaving tail pointer unchanged
2. Which of the following Go code snippets correctly deletes the last node from a doubly linked list named dll?
easy
A. dll.tail = dll.tail.next; dll.tail.prev = nil
B. dll.tail = dll.tail.prev; dll.tail.next = nil
C. dll.head = dll.head.next; dll.head.prev = nil
D. dll.head = dll.tail; dll.tail = nil
Solution
Step 1: Identify correct pointer updates for tail deletion
Deleting last node means tail moves to previous node and new tail's next is nil.
Step 2: Check code correctness
dll.tail = dll.tail.prev; dll.tail.next = nil sets tail to previous node and sets tail.next to nil, which is correct.
Final Answer:
dll.tail = dll.tail.prev; dll.tail.next = nil -> Option B
Quick Check:
Tail moves back, next cleared [OK]
Hint: Tail moves to prev; set tail.next to nil [OK]
Common Mistakes:
Moving tail forward instead of backward
Not setting tail.next to nil
Changing head pointer instead of tail
3. What is the output of the following Go code that deletes the last node from a doubly linked list?
package main
import "fmt"
type Node struct {
data int
prev *Node
next *Node
;
}
type DoublyLinkedList struct {
head *Node
tail *Node
;
}
func (dll *DoublyLinkedList) DeleteFromEnd() {
if dll.tail == nil {
return
}
if dll.tail.prev == nil {
dll.head = nil
dll.tail = nil
return
}
dll.tail = dll.tail.prev
dll.tail.next = nil
}
func (dll *DoublyLinkedList) Print() {
current := dll.head
for current != nil {
fmt.Print(current.data, " -> ")
current = current.next
}
fmt.Println("null")
}
func main() {
n1 := &Node{data: 1}
n2 := &Node{data: 2}
n3 := &Node{data: 3}
n1.next = n2
n2.prev = n1
n2.next = n3
n3.prev = n2
dll := DoublyLinkedList{head: n1, tail: n3}
dll.DeleteFromEnd()
dll.Print()
}
medium
A. 1 -> 2 -> null
B. 1 -> 2 -> 3 -> null
C. 2 -> 3 -> null
D. null
Solution
Step 1: Initial list and deletion
List is 1 -> 2 -> 3 -> null. DeleteFromEnd removes last node (3).
Step 2: After deletion
Tail moves to node with data 2, tail.next set to nil. List becomes 1 -> 2 -> null.
Final Answer:
1 -> 2 -> null -> Option A
Quick Check:
Last node removed, tail updated [OK]
Hint: Deleting end removes last node, tail moves back [OK]
Common Mistakes:
Not updating tail.next to nil causing dangling pointer
Printing old list without deletion
Confusing head and tail pointers
4. The following Go code attempts to delete the last node from a doubly linked list but causes a runtime panic. What is the error?
Hint: Check for single-node list before updating tail [OK]
Common Mistakes:
Not checking if tail.prev is nil before access
Assuming list always has multiple nodes
Ignoring head pointer update when list becomes empty
5. You have a doubly linked list with nodes: 10 <-> 20 <-> 30 <-> 40. After deleting the last node twice using the correct delete from end method, what will be the printed list?
hard
A. 10 -> 20 -> null
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> null
D. 10 -> 30 -> null
Solution
Step 1: Initial list and first deletion
List is 10 <-> 20 <-> 30 <-> 40. First deletion removes 40, list becomes 10 <-> 20 <-> 30.
Step 2: Second deletion
Second deletion removes 30, list becomes 10 <-> 20.
Step 3: Print list
Printing from head: 10 -> 20 -> null.
Final Answer:
10 -> 20 -> null -> Option A
Quick Check:
Two deletions remove last two nodes [OK]
Hint: Each delete removes last node, tail moves back [OK]
Common Mistakes:
Deleting only once instead of twice
Confusing node values after deletion
Not updating tail pointer correctly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.