Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Create and Initialize Doubly Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting nodes in a doubly linked list?
Consider the following JavaScript code that creates a doubly linked list and inserts three nodes. What will be the printed output showing the list from head to tail?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' <-> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const dll = new DoublyLinkedList();
dll.append(10);
dll.append(20);
dll.append(30);
dll.printList();
Anull <-> 10 <-> 20 <-> 30
B30 <-> 20 <-> 10 <-> null
C10 <-> 20 <-> 30 <-> null
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Trace the append method and how nodes link forward and backward.
❓ Predict Output
intermediate
2:00remaining
What is the output after deleting the head node in a doubly linked list?
Given the doubly linked list with nodes 5 <-> 15 <-> 25, what will be the printed output after deleting the head node?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  deleteHead() {
    if (!this.head) return;
    if (this.head === this.tail) {
      this.head = null;
      this.tail = null;
    } else {
      this.head = this.head.next;
      this.head.prev = null;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' <-> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const dll = new DoublyLinkedList();
dll.append(5);
dll.append(15);
dll.append(25);
dll.deleteHead();
dll.printList();
A25 <-> 15 <-> null
B15 <-> 25 <-> null
Cnull
D5 <-> 15 <-> 25 <-> null
Attempts:
2 left
💡 Hint
Deleting head moves head pointer to next node and updates prev to null.
🔧 Debug
advanced
2:00remaining
Identify the bug in the doubly linked list insertion method
The following code tries to insert a new node at the beginning of a doubly linked list. What is the bug that will cause incorrect linking?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  insertAtBeginning(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.next = this.head;
      this.head.prev = newNode;
      this.head = newNode;
      this.tail.prev = newNode; // Bug here
    }
  }
}
AThe line 'this.tail.prev = newNode;' incorrectly sets tail.prev breaking the list
BMissing setting newNode.prev to null causes incorrect links
CNot updating this.tail to newNode causes tail to be wrong
DThe newNode.next should be set to null instead of this.head
Attempts:
2 left
💡 Hint
Tail's prev pointer should not change when inserting at head.
❓ Predict Output
advanced
2:00remaining
What is the output after reversing a doubly linked list?
Given the doubly linked list 1 <-> 2 <-> 3 <-> null, what will be the printed output after reversing the list using the code below?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
    this.prev = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }

  append(value) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }

  reverse() {
    let current = this.head;
    let temp = null;
    while (current) {
      temp = current.prev;
      current.prev = current.next;
      current.next = temp;
      current = current.prev;
    }
    if (temp) {
      this.head = temp.prev;
    }
  }

  printList() {
    let current = this.head;
    let result = '';
    while (current) {
      result += current.value + ' <-> ';
      current = current.next;
    }
    result += 'null';
    console.log(result);
  }
}

const dll = new DoublyLinkedList();
dll.append(1);
dll.append(2);
dll.append(3);
dll.reverse();
dll.printList();
A3 <-> 2 <-> 1 <-> null
B1 <-> 2 <-> 3 <-> null
Cnull
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Reversing swaps next and prev pointers and updates head.
🧠 Conceptual
expert
2:00remaining
How many pointers are updated when inserting a node in the middle of a doubly linked list?
When inserting a new node between two existing nodes in a doubly linked list, how many pointers must be updated to maintain correct links?
A2 pointers
B5 pointers
C3 pointers
D4 pointers
Attempts:
2 left
💡 Hint
Consider next and prev pointers of the new node and its neighbors.

Practice

(1/5)
1. What is the main difference between a doubly linked list and a singly linked list?
easy
A. A doubly linked list has nodes with pointers to both previous and next nodes.
B. A doubly linked list stores data in arrays instead of nodes.
C. A doubly linked list can only be traversed forward.
D. A doubly linked list does not use pointers at all.

Solution

  1. Step 1: Understand node structure in doubly linked list

    Each node in a doubly linked list has two pointers: one to the next node and one to the previous node.
  2. Step 2: Compare with singly linked list

    Singly linked list nodes have only one pointer to the next node, so they cannot go backward.
  3. Final Answer:

    A doubly linked list has nodes with pointers to both previous and next nodes. -> Option A
  4. Quick Check:

    Doubly linked list = two pointers per node [OK]
Hint: Remember: doubly linked means two directions [OK]
Common Mistakes:
  • Thinking doubly linked lists use arrays
  • Believing doubly linked lists only go forward
  • Confusing pointers with data storage
2. Which of the following is the correct way to define a Node class for a doubly linked list in JavaScript?
easy
A. class Node { constructor(data) { this.data = data; this.next = null; } }
B. class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } }
C. class Node { constructor(data) { this.data = data; this.left = null; this.right = null; } }
D. class Node { constructor(data) { this.value = data; this.next = null; } }

Solution

  1. Step 1: Identify required pointers for doubly linked list node

    A doubly linked list node needs both 'prev' and 'next' pointers initialized to null.
  2. Step 2: Check constructor parameters and properties

    class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } correctly defines 'data', 'prev', and 'next' properties in the constructor.
  3. Final Answer:

    class Node { constructor(data) { this.data = data; this.prev = null; this.next = null; } } -> Option B
  4. Quick Check:

    Node needs prev and next pointers [OK]
Hint: Doubly linked nodes have both prev and next [OK]
Common Mistakes:
  • Omitting the prev pointer
  • Using wrong pointer names like left/right
  • Using different property names for data
3. What will be the output of the following code snippet?
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }
  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      newNode.prev = this.tail;
      this.tail = newNode;
    }
  }
}

const list = new DoublyLinkedList();
list.append(1);
list.append(2);
list.append(3);

let current = list.head;
const result = [];
while (current) {
  result.push(current.data);
  current = current.next;
}
console.log(result);
medium
A. []
B. [3, 2, 1]
C. [null, null, null]
D. [1, 2, 3]

Solution

  1. Step 1: Append nodes 1, 2, 3 to the list

    Each append adds a new node at the tail, linking prev and next pointers correctly.
  2. Step 2: Traverse from head to tail collecting data

    Starting at head (1), moving next collects 1, then 2, then 3, then stops at null.
  3. Final Answer:

    [1, 2, 3] -> Option D
  4. Quick Check:

    Traversal from head collects [1, 2, 3] [OK]
Hint: Traverse from head using next to get list order [OK]
Common Mistakes:
  • Confusing traversal direction
  • Not updating tail pointer on append
  • Pushing null values instead of data
4. Identify the error in the following code that tries to append a node to a doubly linked list:
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

class DoublyLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }
  append(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail.prev = newNode;
      this.tail = newNode;
    }
  }
}
medium
A. The constructor is missing initialization of head and tail.
B. The newNode should not have prev or next pointers.
C. The line 'this.tail.prev = newNode;' should be 'newNode.prev = this.tail;'.
D. The append method should not update the tail pointer.

Solution

  1. Step 1: Analyze pointer updates in append method

    When adding newNode, the previous tail's next should point to newNode, and newNode's prev should point to old tail.
  2. Step 2: Identify incorrect pointer assignment

    The code incorrectly sets 'this.tail.prev = newNode;' which wrongly changes the old tail's prev pointer instead of newNode's prev pointer.
  3. Final Answer:

    The line 'this.tail.prev = newNode;' should be 'newNode.prev = this.tail;'. -> Option C
  4. Quick Check:

    Correct prev pointer must link newNode to old tail [OK]
Hint: Prev pointer goes from new node back to old tail [OK]
Common Mistakes:
  • Swapping prev and next pointers
  • Not updating newNode.prev correctly
  • Forgetting to update tail pointer
5. You want to create a doubly linked list from an array of values [10, 20, 30] and then traverse it backward from tail to head. Which code snippet correctly initializes the list and prints the values backward?
hard
A. const list = new DoublyLinkedList(); for (const val of [10, 20, 30]) list.append(val); let current = list.tail; while (current) { console.log(current.data); current = current.prev; }
B. const list = new DoublyLinkedList(); for (let i = 2; i >= 0; i--) list.append([10, 20, 30][i]); let current = list.head; while (current) { console.log(current.data); current = current.next; }
C. const list = new DoublyLinkedList(); for (const val of [10, 20, 30]) list.append(val); let current = list.head; while (current) { console.log(current.data); current = current.prev; }
D. const list = new DoublyLinkedList(); for (const val of [10, 20, 30]) list.append(val); let current = list.tail; while (current) { console.log(current.data); current = current.next; }

Solution

  1. Step 1: Append values in order to the list

    Using a for-of loop appends 10, then 20, then 30, building the list head to tail.
  2. Step 2: Traverse backward from tail using prev pointer

    Starting at tail (30), moving prev prints 30, 20, 10 in reverse order.
  3. Final Answer:

    const list = new DoublyLinkedList(); for (const val of [10, 20, 30]) list.append(val); let current = list.tail; while (current) { console.log(current.data); current = current.prev; } -> Option A
  4. Quick Check:

    Backward traversal uses tail and prev pointer [OK]
Hint: Traverse backward from tail using prev pointer [OK]
Common Mistakes:
  • Traversing backward from head
  • Using next pointer to go backward
  • Appending values in reverse order but traversing forward
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard