📖 Scenario: You are building a simple playlist manager. Each song is a node in a doubly linked list. You want to add new songs to the start of the playlist so they play first.
🎯 Goal: Create a doubly linked list and write code to insert a new song at the beginning. Then print the playlist from start to end.
📋 What You'll Learn
Create a Node class with data, prev, and next properties
Create a DoublyLinkedList class with a head property
Write a method insertAtBeginning(data) to add a new node at the start
Write a method printList() to print all nodes from head to end
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history), and undo-redo features where you need to move forward and backward easily.
💼 Career
Understanding linked lists helps in software development roles that involve data structure optimization and memory management.
Progress0 / 4 steps
1
Create Node and DoublyLinkedList classes
Create a class called Node with properties data, prev, and next. Then create a class called DoublyLinkedList with a property head initialized to null.
DSA Javascript
Hint
Think of Node as a box holding a song name and links to previous and next songs. The DoublyLinkedList starts empty with head as null.
2
Add insertAtBeginning method
Inside the DoublyLinkedList class, add a method called insertAtBeginning(data). This method should create a new Node with the given data. If the list is empty, set head to this new node. Otherwise, set the new node's next to the current head, set the current head's prev to the new node, and update head to the new node.
DSA Javascript
Hint
Remember to update both next and prev links when inserting at the start.
3
Add printList method
Inside the DoublyLinkedList class, add a method called printList(). This method should start from head and traverse the list using next pointers. Collect the data of each node in an array. Then join the array elements with ' <-> ' and print the resulting string.
DSA Javascript
Hint
Use a loop to move from one node to the next until you reach the end (null).
4
Insert songs and print playlist
Create a new DoublyLinkedList called playlist. Use insertAtBeginning to add these songs in order: 'Song A', 'Song B', 'Song C'. Then call printList() on playlist to display the playlist.
DSA Javascript
Hint
Remember, inserting at the beginning means the last inserted song appears first when printing.
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 remove the first node from 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, making it the new head.
Final Answer:
To add a new node before the current head node -> Option A
Quick Check:
Insert at beginning = add before head [OK]
Hint: New node becomes new head in doubly linked list [OK]
Common Mistakes:
Confusing insertion at beginning with insertion at end
Thinking insertion removes nodes
Mixing up head and tail nodes
2. Which of the following is the correct way to update the previous pointer of the old head node when inserting a new node at the beginning in JavaScript?
easy
A. oldHead.prev = newNode;
B. newNode.next = oldHead.prev;
C. newNode.prev = oldHead;
D. oldHead.next = newNode;
Solution
Step 1: Identify the old head's previous pointer
The old head's previous pointer should point to the new node after insertion.
Step 2: Update old head's prev to new node
Setting oldHead.prev = newNode links the old head back to the new node.
Final Answer:
oldHead.prev = newNode; -> Option A
Quick Check:
Old head prev points to new node [OK]
Hint: Old head's prev points to new node after insertion [OK]
Common Mistakes:
Setting oldHead.next instead of prev
Confusing newNode's pointers
Assigning newNode.next incorrectly
3. What will be the printed state of the doubly linked list after inserting a node with value 10 at the beginning of the list 5 <-> 15 <-> 20?
medium
A. 5 <-> 15 <-> 20 <-> 10
B. 10 <-> 15 <-> 20 <-> 5
C. 10 <-> 5 <-> 15 <-> 20
D. 5 <-> 10 <-> 15 <-> 20
Solution
Step 1: Insert 10 before current head (5)
The new node with value 10 becomes the new head, pointing next to 5.
Step 2: Link the rest of the list after 5
The list continues as 5 <-> 15 <-> 20 unchanged after insertion.
Final Answer:
10 <-> 5 <-> 15 <-> 20 -> Option C
Quick Check:
New head 10 followed by old list [OK]
Hint: New node appears first, old list follows [OK]
Common Mistakes:
Appending new node at end instead of beginning
Swapping nodes incorrectly
Ignoring pointer updates
4. Given the following code snippet for inserting at the beginning of a doubly linked list, what is the error?
function insertAtBeginning(head, data) {
const newNode = { data: data, prev: null, next: head };
head.prev = newNode;
head = newNode;
return head;
}
medium
A. It incorrectly sets newNode.next to null
B. It does not handle empty list when head is null
C. It forgets to update newNode.prev to head
D. It returns the old head instead of new head
Solution
Step 1: Check for empty list case
If head is null, head.prev causes error because head is null.
Step 2: Missing null check before setting head.prev
Code should check if head exists before setting head.prev = newNode.
Final Answer:
It does not handle empty list when head is null -> Option B
Quick Check:
Null head requires special handling [OK]
Hint: Always check if head is null before accessing its properties [OK]
Common Mistakes:
Ignoring empty list case
Assuming head is never null
Not returning updated head
5. You have a doubly linked list with nodes: 7 <-> 14 <-> 21. You want to insert a new node with value 3 at the beginning. After insertion, what will be the correct sequence of node values when traversing from head to tail and tail to head?
hard
A. Head to tail: 21 <-> 14 <-> 7 <-> 3; Tail to head: 3 <-> 7 <-> 14 <-> 21
B. Head to tail: 7 <-> 14 <-> 21 <-> 3; Tail to head: 3 <-> 21 <-> 14 <-> 7
C. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 3 <-> 7 <-> 14 <-> 21
D. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3
Solution
Step 1: Insert 3 before current head 7
The new node 3 becomes the head, pointing next to 7.