Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Insert at Beginning of 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 10 at the beginning?
Consider a doubly linked list initially containing nodes with values 20 and 30. We insert a new node with value 10 at the beginning. What will be the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtBeginning(data) {
    const newNode = new Node(data);
    if (this.head === null) {
      this.head = newNode;
      return;
    }
    newNode.next = this.head;
    this.head.prev = newNode;
    this.head = newNode;
  }

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

const dll = new DoublyLinkedList();
dll.insertAtBeginning(20);
dll.insertAtBeginning(30);
dll.insertAtBeginning(10);
dll.printList();
A20 -> 30 -> 10 -> null
B10 -> 20 -> 30 -> null
C10 -> 30 -> 20 -> null
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the new node becomes the head, pushing existing nodes to the right.
❓ Predict Output
intermediate
2:00remaining
What is the output after inserting 5 at the beginning of an empty list?
We start with an empty doubly linked list and insert a node with value 5 at the beginning. What will be the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtBeginning(data) {
    const newNode = new Node(data);
    if (this.head === null) {
      this.head = newNode;
      return;
    }
    newNode.next = this.head;
    this.head.prev = newNode;
    this.head = newNode;
  }

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

const dll = new DoublyLinkedList();
dll.insertAtBeginning(5);
dll.printList();
Anull
B5 -> null
C0 -> 5 -> null
D5 -> 0 -> null
Attempts:
2 left
💡 Hint
When the list is empty, the new node becomes the only node and head.
🔧 Debug
advanced
2:00remaining
Identify the error in this insertAtBeginning method
The following insertAtBeginning method is intended to add a new node at the start of a doubly linked list. What error will it cause when called on a non-empty list?
DSA Javascript
insertAtBeginning(data) {
  const newNode = new Node(data);
  newNode.next = this.head;
  this.head.prev = newNode;
  this.head = newNode;
  newNode.prev = this.head;
}
ACreates a circular reference causing infinite loop on traversal
BNo error; works correctly
CThrows TypeError because this.head is null
DLeaves the previous head's prev pointer null causing broken backward links
Attempts:
2 left
💡 Hint
Check the last line where newNode.prev is assigned.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple insertions at beginning?
Starting with an empty doubly linked list, we insert 1, then 2, then 3 at the beginning. What will be the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtBeginning(data) {
    const newNode = new Node(data);
    if (this.head === null) {
      this.head = newNode;
      return;
    }
    newNode.next = this.head;
    this.head.prev = newNode;
    this.head = newNode;
  }

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

const dll = new DoublyLinkedList();
dll.insertAtBeginning(1);
dll.insertAtBeginning(2);
dll.insertAtBeginning(3);
dll.printList();
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
C1 -> 3 -> 2 -> null
D2 -> 3 -> 1 -> null
Attempts:
2 left
💡 Hint
Each new insertion at beginning becomes the new head.
🧠 Conceptual
expert
2:00remaining
Why is updating the prev pointer important when inserting at beginning?
When inserting a new node at the beginning of a doubly linked list, why must we update the previous head node's prev pointer to the new node?
ATo avoid memory leaks by freeing old pointers
BTo prevent the new node from being garbage collected
CTo speed up insertion by skipping the next pointer update
DTo maintain the backward link so traversal from tail to head works correctly
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backward.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To add a new node before the current head node
B. To add a new node after the tail node
C. To remove the first node from the list
D. To reverse the entire list

Solution

  1. Step 1: Understand the head node position

    The head node is the first node in the doubly linked list.
  2. Step 2: Insert new node before head

    Inserting at the beginning means placing the new node before the current head, making it the new head.
  3. Final Answer:

    To add a new node before the current head node -> Option A
  4. Quick Check:

    Insert at beginning = add before head [OK]
Hint: New node becomes new head in doubly linked list [OK]
Common Mistakes:
  • Confusing insertion at beginning with insertion at end
  • Thinking insertion removes nodes
  • Mixing up head and tail nodes
2. Which of the following is the correct way to update the previous pointer of the old head node when inserting a new node at the beginning in JavaScript?
easy
A. oldHead.prev = newNode;
B. newNode.next = oldHead.prev;
C. newNode.prev = oldHead;
D. oldHead.next = newNode;

Solution

  1. Step 1: Identify the old head's previous pointer

    The old head's previous pointer should point to the new node after insertion.
  2. Step 2: Update old head's prev to new node

    Setting oldHead.prev = newNode links the old head back to the new node.
  3. Final Answer:

    oldHead.prev = newNode; -> Option A
  4. Quick Check:

    Old head prev points to new node [OK]
Hint: Old head's prev points to new node after insertion [OK]
Common Mistakes:
  • Setting oldHead.next instead of prev
  • Confusing newNode's pointers
  • Assigning newNode.next incorrectly
3. What will be the printed state of the doubly linked list after inserting a node with value 10 at the beginning of the list 5 <-> 15 <-> 20?
medium
A. 5 <-> 15 <-> 20 <-> 10
B. 10 <-> 15 <-> 20 <-> 5
C. 10 <-> 5 <-> 15 <-> 20
D. 5 <-> 10 <-> 15 <-> 20

Solution

  1. Step 1: Insert 10 before current head (5)

    The new node with value 10 becomes the new head, pointing next to 5.
  2. Step 2: Link the rest of the list after 5

    The list continues as 5 <-> 15 <-> 20 unchanged after insertion.
  3. Final Answer:

    10 <-> 5 <-> 15 <-> 20 -> Option C
  4. Quick Check:

    New head 10 followed by old list [OK]
Hint: New node appears first, old list follows [OK]
Common Mistakes:
  • Appending new node at end instead of beginning
  • Swapping nodes incorrectly
  • Ignoring pointer updates
4. Given the following code snippet for inserting at the beginning of a doubly linked list, what is the error?
function insertAtBeginning(head, data) {
  const newNode = { data: data, prev: null, next: head };
  head.prev = newNode;
  head = newNode;
  return head;
}
medium
A. It incorrectly sets newNode.next to null
B. It does not handle empty list when head is null
C. It forgets to update newNode.prev to head
D. It returns the old head instead of new head

Solution

  1. Step 1: Check for empty list case

    If head is null, head.prev causes error because head is null.
  2. Step 2: Missing null check before setting head.prev

    Code should check if head exists before setting head.prev = newNode.
  3. Final Answer:

    It does not handle empty list when head is null -> Option B
  4. Quick Check:

    Null head requires special handling [OK]
Hint: Always check if head is null before accessing its properties [OK]
Common Mistakes:
  • Ignoring empty list case
  • Assuming head is never null
  • Not returning updated head
5. You have a doubly linked list with nodes: 7 <-> 14 <-> 21. You want to insert a new node with value 3 at the beginning. After insertion, what will be the correct sequence of node values when traversing from head to tail and tail to head?
hard
A. Head to tail: 21 <-> 14 <-> 7 <-> 3; Tail to head: 3 <-> 7 <-> 14 <-> 21
B. Head to tail: 7 <-> 14 <-> 21 <-> 3; Tail to head: 3 <-> 21 <-> 14 <-> 7
C. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 3 <-> 7 <-> 14 <-> 21
D. Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3

Solution

  1. Step 1: Insert 3 before current head 7

    The new node 3 becomes the head, pointing next to 7.
  2. Step 2: Traverse forward and backward

    Forward traversal: 3 -> 7 -> 14 -> 21; backward traversal reverses this order.
  3. Final Answer:

    Head to tail: 3 <-> 7 <-> 14 <-> 21; Tail to head: 21 <-> 14 <-> 7 <-> 3 -> Option D
  4. Quick Check:

    New head first, tail last, pointers reversed backward [OK]
Hint: Head to tail is forward order; tail to head is reverse [OK]
Common Mistakes:
  • Mixing forward and backward traversal order
  • Appending new node at tail instead of head
  • Incorrect pointer updates causing wrong traversal
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