Which of the following is a key advantage of a doubly linked list over a singly linked list?
Think about how you can move through the list from both ends.
Doubly linked lists have two pointers per node, allowing easy movement forward and backward. Singly linked lists only allow forward traversal.
Given a doubly linked list with nodes containing values 1, 2, 3 linked in order, what is the output when traversing backwards from the tail?
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } // Creating nodes const n1 = new Node(1); const n2 = new Node(2); const n3 = new Node(3); // Linking nodes n1.next = n2; n2.prev = n1; n2.next = n3; n3.prev = n2; // Traverse backwards from tail let current = n3; const result = []; while (current) { result.push(current.value); current = current.prev; } console.log(result.join(' -> ') + ' -> null');
Start from the last node and move using the prev pointer.
Traversing backwards from the tail visits nodes 3, then 2, then 1.
What is the bug in the following code that inserts a new node after a given node in a doubly linked list?
function insertAfter(node, newNode) {
newNode.next = node.next;
node.next = newNode;
newNode.prev = node;
// Missing update for the next node's prev pointer
}Check if all pointers are updated correctly after insertion.
When inserting, the next node's prev pointer must point back to the new node to maintain list integrity.
What is the output after deleting the middle node (value 2) from the doubly linked list 1 <-> 2 <-> 3?
class Node { constructor(value) { this.value = value; this.next = null; this.prev = null; } } const n1 = new Node(1); const n2 = new Node(2); const n3 = new Node(3); n1.next = n2; n2.prev = n1; n2.next = n3; n3.prev = n2; // Delete node n2 n1.next = n3; n3.prev = n1; // Traverse forward let current = n1; const result = []; while (current) { result.push(current.value); current = current.next; } console.log(result.join(' -> ') + ' -> null');
After deletion, the middle node is removed and pointers updated.
Deleting node 2 links node 1 directly to node 3, so traversal shows 1 -> 3 -> null.
Which scenario best explains why a doubly linked list is preferred over a singly linked list?
Think about operations that need to move backwards or remove nodes easily.
Doubly linked lists allow efficient backward traversal and easy deletion from both ends, unlike singly linked lists.
