📖 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 end of the playlist.
🎯 Goal: Create a doubly linked list and write code to insert a new node at the end. Then print the list from head to tail showing the song names.
📋 What You'll Learn
Create a Node class with data, prev, and next properties
Create a DoublyLinkedList class with head and tail properties
Write a method insertAtEnd(data: string) to add a new node at the end
Write a method printList() to print the list from head to tail
💡 Why This Matters
🌍 Real World
Doubly linked lists are used in music players, browsers (history), and other apps where you need to move forward and backward through items.
💼 Career
Understanding linked lists helps in software engineering roles that involve data structure design, memory management, and building efficient algorithms.
Progress0 / 4 steps
1
Create Node and DoublyLinkedList classes
Create a Node class with data (string), prev (Node or null), and next (Node or null). Then create a DoublyLinkedList class with head and tail properties initialized to null.
DSA Typescript
Hint
Define Node with constructor setting data, prev, and next. Define DoublyLinkedList with head and tail set to null.
2
Add insertAtEnd method
Inside the DoublyLinkedList class, add a method insertAtEnd(data: string): void that creates a new Node with data and inserts it at the end of the list. Update head and tail accordingly.
DSA Typescript
Hint
Create a new node. If list is empty, set head and tail to new node. Otherwise, link new node after tail and update tail.
3
Add printList method
Inside the DoublyLinkedList class, add a method printList(): void that prints the list from head to tail. Print the data of each node separated by -> and end with null.
DSA Typescript
Hint
Start from head, loop while node is not null, add data and arrow to a string, then print it.
4
Insert songs and print the playlist
Create a DoublyLinkedList instance called playlist. Insert these songs at the end in order: 'Song A', 'Song B', 'Song C'. Then call playlist.printList() to print the playlist.
DSA Typescript
Hint
Create playlist as new DoublyLinkedList(). Use insertAtEnd to add songs in order. Call printList() to show 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 add a new element after the last node
B. To remove the first node
C. To reverse the list
D. To find the middle node
Solution
Step 1: Understand the structure of a doubly linked list
A doubly linked list has nodes connected forward and backward, 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, updating pointers accordingly.
Final Answer:
To add a new element after the last node -> Option A
Quick Check:
Insert at end = add after last node [OK]
Hint: Insert at end means add after last node [OK]
Common Mistakes:
Confusing insertion at end with deletion
Thinking insertion reverses the list
Assuming insertion happens at the start
2. Which of the following TypeScript code snippets correctly initializes a new node for a doubly linked list?
Hint: Node needs data, next, and prev properties [OK]
Common Mistakes:
Omitting prev pointer
Using wrong property names like value or nextNode
Setting next or prev as undefined instead of null
3. Given the following TypeScript code for inserting at the end of a doubly linked list, what will be the printed list after insertion?
class Node {
data: number;
next: Node | null = null;
prev: Node | null = null;
constructor(data: number) { this.data = data; }
}
class DoublyLinkedList {
head: Node | null = null;
insertAtEnd(data: number) {
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. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. 10 -> 20 -> null
D. null
Solution
Step 1: Trace insertAtEnd calls
First insert adds 10 as head. Second adds 20 after 10. Third adds 30 after 20.
Step 2: Trace printList output
Print starts at head (10), then 20, then 30, then null.
Final Answer:
10 -> 20 -> 30 -> null -> Option A
Quick Check:
Inserted nodes in order = 10 -> 20 -> 30 [OK]
Hint: Insertion at end appends nodes in order [OK]
Common Mistakes:
Confusing order and printing reversed list
Missing last inserted node
Printing null instead of list
4. Identify the error in the following TypeScript method for inserting at the end of a doubly linked list:
insertAtEnd(data: number) {
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. newNode should be created outside the method
B. Incorrect assignment of newNode.prev
C. Loop condition should be temp !== null
D. Missing return after setting head when list is empty
Solution
Step 1: Analyze empty list case
If head is null, method sets head to newNode but continues to loop and assign next, causing error.
Step 2: Fix by adding return after setting head
Adding return after setting head prevents further code execution and avoids null pointer errors.
Final Answer:
Missing return after setting head when list is empty -> Option D
Quick Check:
Return after head set avoids errors [OK]
Hint: Return immediately after setting head if list empty [OK]
Common Mistakes:
Not returning after setting head
Misunderstanding loop condition
Incorrect pointer assignments
5. You want to insert a new node with data 40 at the end of a doubly linked list that currently has nodes 10 <-> 20 <-> 30. After insertion, what will be the correct state of the list including the prev and next pointers of the new node?
hard
A. 10 <-> 20 <-> 30, with 40.prev and 40.next both null (not linked)
B. 40 <-> 10 <-> 20 <-> 30, where 40.next points to 10 and 40.prev is null
C. 10 <-> 20 <-> 30 <-> 40, where 40.prev points to 30 and 40.next is null
D. 10 <-> 20 <-> 30 <-> 40, where 40.prev and 40.next both point to null
Solution
Step 1: Understand insertion at end
The new node 40 should be linked after 30, so 30.next = 40 and 40.prev = 30.
Step 2: Confirm pointers of new node
Since 40 is last, 40.next must be null to mark list end.
Final Answer:
10 <-> 20 <-> 30 <-> 40, where 40.prev points to 30 and 40.next is null -> Option C
Quick Check:
New tail prev = old tail, next = null [OK]
Hint: New tail node prev = old tail, next = null [OK]
Common Mistakes:
Setting new node's next pointer incorrectly
Not linking new node's prev pointer
Inserting new node at start instead of end
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.