📖 Scenario: You are managing a playlist of songs using a doubly linked list. Each song is a node with a title and links to the previous and next songs. You want to insert a new song at a specific position in the playlist.
🎯 Goal: Build a doubly linked list with initial songs, set the position to insert a new song, insert the new song at that position, and print the playlist showing song titles in order.
📋 What You'll Learn
Create a doubly linked list with three songs: 'Song A', 'Song B', 'Song C'
Create a variable position with value 2 to indicate where to insert
Insert a new song node with title 'Song X' at the position in the doubly linked list
Print the playlist titles from head to tail separated by ' -> ' and ending with ' -> 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 lists and insertion operations is fundamental for software engineering roles involving data structures and memory management.
Progress0 / 4 steps
1
Create the initial doubly linked list
Create a doubly linked list with nodes for songs titled 'Song A', 'Song B', and 'Song C'. Use a class Node with properties title, prev, and next. Create variables head and tail to represent the start and end of the list.
DSA Javascript
Hint
Define a Node class with title, prev, and next. Then create three nodes and link them in order.
2
Set the position to insert the new song
Create a variable called position and set it to the number 2. This will be the position where the new song will be inserted (positions start at 1).
DSA Javascript
Hint
Just create a variable position and assign it the value 2.
3
Insert the new song at the specified position
Create a new node with title 'Song X'. Insert this new node at the position in the doubly linked list. Update the prev and next pointers of the surrounding nodes to keep the list connected.
DSA Javascript
Hint
Traverse the list to the node before the position. Insert the new node by adjusting next and prev pointers of the new node and its neighbors.
4
Print the playlist from head to tail
Use a while loop to traverse the doubly linked list from head to the end. Print the title of each node separated by ' -> ' and end with 'null'.
DSA Javascript
Hint
Start from head and keep moving to next nodes, adding titles to a string. Print the final string.
Practice
(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node becomes the new head of the list.
B. The new node is added at the end of the list.
C. The new node replaces the last node.
D. The list remains unchanged.
Solution
Step 1: Understand position 1 in doubly linked list
Position 1 means the start of the list, so the new node should be the first node.
Step 2: Effect of insertion at position 1
The new node's next pointer points to the old head, and the old head's previous pointer points back to the new node. The new node becomes the head.
Final Answer:
The new node becomes the new head of the list. -> Option A
Quick Check:
Insert at position 1 = new head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
Thinking position 1 means end insertion
Not updating previous pointer of old head
Ignoring head pointer update
2. Which of the following is the correct syntax to insert a new node at position pos in a doubly linked list in JavaScript?
Hint: Next and prev pointers must be updated symmetrically [OK]
Common Mistakes:
Mixing prev and next pointers
Not updating current.next.prev
Incorrect order of assignments causing broken links
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what is the list after inserting 15 at position 2 using correct insertion logic?
medium
A. 10 <-> 20 <-> 30 <-> 15 <-> null
B. 15 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 15 <-> 20 <-> 30 <-> null
D. 10 <-> 20 <-> 15 <-> 30 <-> null
Solution
Step 1: Identify position 2 in the list
Position 2 means after the first node (10), so 15 should be inserted between 10 and 20.
Step 2: Insert 15 between 10 and 20
Update pointers so 10 points to 15, 15 points to 20, and 20's prev points to 15.
Final Answer:
10 <-> 15 <-> 20 <-> 30 <-> null -> Option C
Quick Check:
Insert at position 2 = between first and second node [OK]
Hint: Position 2 means after first node [OK]
Common Mistakes:
Inserting at wrong position
Not updating prev pointer of next node
Breaking list links after insertion
4. Consider this code snippet to insert a node at position 3 in a doubly linked list:
let current = head;
for(let i = 1; i < pos - 1; i++) {
current = current.next;
}
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
What error might occur if pos is greater than the list length?
medium
A. The new node is inserted at the head incorrectly.
B. Runtime error due to accessing next of null.
C. The list becomes circular causing infinite loop.
D. No error; insertion happens at the end automatically.
Solution
Step 1: Analyze loop for position traversal
The loop moves current forward pos-2 times. If pos is too large, current becomes null.
Step 2: Accessing current.next when current is null
Trying to access current.next.prev causes runtime error because current is null.
Final Answer:
Runtime error due to accessing next of null. -> Option B
Quick Check:
Out-of-range position causes null access error [OK]
Hint: Check list length before insertion [OK]
Common Mistakes:
Not validating position against list length
Assuming insertion at end without checks
Ignoring null pointer exceptions
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> null. You want to insert a new node with value 12 at position 3. After insertion, you also want to remove the node at position 4. What will be the final list?
hard
A. 5 <-> 12 <-> 10 <-> 20 <-> null
B. 5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null
C. 5 <-> 10 <-> 15 <-> 20 <-> null
D. 5 <-> 10 <-> 12 <-> 20 <-> null
Solution
Step 1: Insert 12 at position 3
Position 3 means between nodes with values 10 and 15. After insertion: 5 <-> 10 <-> 12 <-> 15 <-> 20.
Step 2: Remove node at position 4
Position 4 now points to node with value 15. Removing it links 12 directly to 20.
Final Answer:
5 <-> 10 <-> 12 <-> 20 <-> null -> Option D
Quick Check:
Insert then remove node at position 4 = 5 <-> 10 <-> 12 <-> 20 <-> null [OK]
Hint: Insert first, then remove target node [OK]
Common Mistakes:
Removing wrong node after insertion
Not updating pointers after removal
Confusing positions after insertion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.