📖 Scenario: You are building a simple music playlist app. Each song is a node in a doubly linked list. You want to add new songs to the end of the playlist.
🎯 Goal: Create a doubly linked list and write code to insert a new song at the end. 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 head and tail properties
Add a method insertAtEnd(data) to add a new node at the end
Add a method printList() to print the list from head to tail in the format: Song1 <-> Song2 <-> Song3
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history), and many apps where you need to move forward and backward through items.
💼 Career
Understanding linked lists is fundamental for software developers, especially for roles involving data structures, algorithms, and system design.
Progress0 / 4 steps
1
Create Node and DoublyLinkedList classes
Create a Node class with a constructor that takes data and sets prev and next to null. Create a DoublyLinkedList class with head and tail properties initialized to null.
DSA Javascript
Hint
Define two classes: Node and DoublyLinkedList. Initialize properties as described.
2
Add insertAtEnd method
Add a method called insertAtEnd(data) inside the DoublyLinkedList class. It should create a new Node with data. If the list is empty, set both head and tail to the new node. Otherwise, link the new node after tail and update tail.
DSA Javascript
Hint
Check if the list is empty. If yes, set head and tail to the new node. Otherwise, link the new node after tail and update tail.
3
Add printList method
Add a method called printList() inside the DoublyLinkedList class. It should start from head and collect all data values in an array. Then join them with ' <-> ' and print the result.
DSA Javascript
Hint
Use a loop to go through nodes starting at head. Collect data in an array and print joined by ' <-> '.
4
Insert songs and print playlist
Create a new DoublyLinkedList called playlist. Use insertAtEnd to add these songs in order: 'Song A', 'Song B', 'Song C'. Then call printList() on playlist to display the playlist.
DSA Javascript
Hint
Create the playlist, add the three songs using insertAtEnd, then print the list.
Practice
(1/5)
1. What is the main purpose of inserting a node at the end of a doubly linked list?
easy
A. To reverse the order of the nodes in the list
B. To remove the first node from the list
C. To add a new element after the last node, updating both next and previous links
D. To find the middle node of the list
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes with pointers to both the next and previous nodes, allowing traversal in both directions.
Step 2: Purpose of insertion at the end
Inserting at the end means adding a new node after the current last node and updating the pointers accordingly.
Final Answer:
To add a new element after the last node, updating both next and previous links -> Option C
Quick Check:
Insert at end = Add after last node [OK]
Hint: Insert at end means add after last node with correct links [OK]
Common Mistakes:
Confusing insertion at end with deletion
Not updating previous pointer of new node
Trying to insert at beginning instead
2. Which of the following is the correct syntax to create a new node in JavaScript for a doubly linked list?
easy
A. const newNode = new Node(value);
B. const newNode = { value: data, nextNode: null };
Hint: Node needs data, next, and prev keys set [OK]
Common Mistakes:
Using wrong property names like value instead of data
Missing prev pointer in node
Using array instead of object for node
3. Consider the following code snippet that inserts nodes at the end of a doubly linked list. What will be the printed list after inserting 10, 20, and 30?
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
constructor() {
this.head = null;
}
insertAtEnd(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let temp = this.head;
while (temp.next) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
printList() {
let temp = this.head;
let result = '';
while (temp) {
result += temp.data + ' -> ';
temp = temp.next;
}
result += 'null';
console.log(result);
}
}
const dll = new DoublyLinkedList();
dll.insertAtEnd(10);
dll.insertAtEnd(20);
dll.insertAtEnd(30);
dll.printList();
medium
A. null
B. 10 -> 20 -> 30 -> null
C. 10 -> 20 -> null
D. 30 -> 20 -> 10 -> null
Solution
Step 1: Insert nodes one by one at the end
First insert 10 as head, then 20 after 10, then 30 after 20.
Step 2: Print the list from head to end
Traverse from head: 10 -> 20 -> 30 -> null is printed.
Final Answer:
10 -> 20 -> 30 -> null -> Option B
Quick Check:
Insert order = print order [OK]
Hint: Insert at end keeps order; print from head shows insertion order [OK]
Common Mistakes:
Confusing insertion order with reversed output
Not updating prev pointer causing broken links
Printing from tail instead of head
4. The following code tries to insert a node at the end of a doubly linked list but causes an error. What is the mistake?
insertAtEnd(data) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
}
let temp = this.head;
while (temp.next) {
temp = temp.next;
}
temp.next = newNode;
newNode.prev = temp;
}
medium
A. Missing return after setting head when list is empty
B. Incorrectly assigning newNode.prev instead of newNode.next
C. Not creating newNode with correct constructor
D. Using while loop instead of for loop
Solution
Step 1: Analyze the empty list case
If head is null, newNode is assigned as head but code continues to traverse and assign next pointers.
Without return after setting head, code tries to add newNode again after head, causing a cycle or error.
Final Answer:
Missing return after setting head when list is empty -> Option A
Quick Check:
Return after empty insert prevents duplicate links [OK]
Hint: Return immediately after empty list insertion [OK]
Common Mistakes:
Forgetting to stop function after empty list insert
Mixing up prev and next pointers
Assuming loop type causes error
5. You want to insert a node with value 40 at the end of a doubly linked list that currently has nodes 10 -> 20 -> 30 -> null. After insertion, what should be the correct state of the list and the pointers of the new node?
hard
A. 10 -> 20 -> 30 -> 40 -> null, newNode.prev points to 30, newNode.next is null
B. 40 -> 10 -> 20 -> 30 -> null, newNode.prev is null, newNode.next points to 10
C. 10 -> 20 -> 30 -> null, newNode.prev and newNode.next both null
D. 10 -> 20 -> 30 -> 40 -> null, newNode.prev is null, newNode.next points to 30
Solution
Step 1: Insert new node at the end
New node with value 40 is added after node 30, so 30's next points to new node.
Step 2: Set new node's pointers correctly
New node's prev points to 30, and next is null because it's the last node.
Final Answer:
10 -> 20 -> 30 -> 40 -> null, newNode.prev points to 30, newNode.next is null -> Option A
Quick Check:
Last node's next = null, newNode.prev = previous last node [OK]
Hint: New last node prev = old last node, next = null [OK]
Common Mistakes:
Setting newNode.prev to null incorrectly
Placing new node at the beginning instead
Not updating previous last node's next pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.