Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Insert at Specific Position in Doubly Linked List in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Doubly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after inserting at position 2
What is the printed state of the doubly linked list after inserting value 99 at position 2?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtPosition(data, position) {
    const newNode = new Node(data);
    if (position === 1) {
      newNode.next = this.head;
      if (this.head) this.head.prev = newNode;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current && count < position - 1) {
      current = current.next;
      count++;
    }
    if (!current) return; // position out of bounds
    newNode.next = current.next;
    if (current.next) current.next.prev = newNode;
    current.next = newNode;
    newNode.prev = current;
  }

  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.insertAtPosition(10, 1);
dll.insertAtPosition(20, 2);
dll.insertAtPosition(30, 3);
dll.insertAtPosition(99, 2);
dll.printList();
A10 <-> 99 <-> 20 <-> 30 <-> null
B10 <-> 20 <-> 99 <-> 30 <-> null
C99 <-> 10 <-> 20 <-> 30 <-> null
D10 <-> 20 <-> 30 <-> 99 <-> null
Attempts:
2 left
💡 Hint
Remember that position 1 means inserting at the head, and position 2 means after the first node.
❓ Predict Output
intermediate
2:00remaining
Output after inserting at head position
What is the printed state of the doubly linked list after inserting value 5 at position 1?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtPosition(data, position) {
    const newNode = new Node(data);
    if (position === 1) {
      newNode.next = this.head;
      if (this.head) this.head.prev = newNode;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current && count < position - 1) {
      current = current.next;
      count++;
    }
    if (!current) return; // position out of bounds
    newNode.next = current.next;
    if (current.next) current.next.prev = newNode;
    current.next = newNode;
    newNode.prev = current;
  }

  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.insertAtPosition(10, 1);
dll.insertAtPosition(20, 2);
dll.insertAtPosition(30, 3);
dll.insertAtPosition(5, 1);
dll.printList();
A10 <-> 5 <-> 20 <-> 30 <-> null
B10 <-> 20 <-> 30 <-> 5 <-> null
C5 <-> 20 <-> 10 <-> 30 <-> null
D5 <-> 10 <-> 20 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Inserting at position 1 means adding at the start of the list.
🔧 Debug
advanced
2:00remaining
Identify the error in insertAtPosition method
What error will occur when trying to insert at a position greater than the list length using this code?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtPosition(data, position) {
    const newNode = new Node(data);
    if (position === 1) {
      newNode.next = this.head;
      if (this.head) this.head.prev = newNode;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current && count < position - 1) {
      current = current.next;
      count++;
    }
    // Missing check if current is null here
    newNode.next = current.next;
    if (current.next) current.next.prev = newNode;
    current.next = newNode;
    newNode.prev = current;
  }
}

const dll = new DoublyLinkedList();
dll.insertAtPosition(10, 1);
dll.insertAtPosition(20, 2);
dll.insertAtPosition(30, 3);
dll.insertAtPosition(40, 5);
ATypeError: Cannot read property 'next' of null
BSyntaxError: Unexpected token
CNo error, insertion happens at the end
DReferenceError: current is not defined
Attempts:
2 left
💡 Hint
Check what happens if position is beyond the list length and current becomes null.
❓ Predict Output
advanced
2:00remaining
Output after inserting at last position
What is the printed state of the doubly linked list after inserting value 40 at position 4?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.prev = null;
    this.next = null;
  }
}

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

  insertAtPosition(data, position) {
    const newNode = new Node(data);
    if (position === 1) {
      newNode.next = this.head;
      if (this.head) this.head.prev = newNode;
      this.head = newNode;
      return;
    }
    let current = this.head;
    let count = 1;
    while (current && count < position - 1) {
      current = current.next;
      count++;
    }
    if (!current) return;
    newNode.next = current.next;
    if (current.next) current.next.prev = newNode;
    current.next = newNode;
    newNode.prev = current;
  }

  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.insertAtPosition(10, 1);
dll.insertAtPosition(20, 2);
dll.insertAtPosition(30, 3);
dll.insertAtPosition(40, 4);
dll.printList();
A40 <-> 10 <-> 20 <-> 30 <-> null
B10 <-> 20 <-> 30 <-> 40 <-> null
C10 <-> 20 <-> 40 <-> 30 <-> null
D10 <-> 20 <-> 30 <-> null
Attempts:
2 left
💡 Hint
Inserting at position 4 means adding after the third node, which is the current last node.
🧠 Conceptual
expert
2:00remaining
Why is updating prev pointer important in doubly linked list insertion?
When inserting a new node in a doubly linked list, why must we update the prev pointer of the node after the inserted node?
ATo ensure the list remains sorted automatically
BTo prevent memory leaks in the list
CTo maintain the backward link so traversal from tail to head works correctly
DTo avoid duplicate nodes in the list
Attempts:
2 left
💡 Hint
Think about how doubly linked lists allow moving backwards.

Practice

(1/5)
1. What happens when you insert a new node at position 1 in a doubly linked list?
easy
A. The new node becomes the new head of the list.
B. The new node is added at the end of the list.
C. The new node replaces the last node.
D. The list remains unchanged.

Solution

  1. Step 1: Understand position 1 in doubly linked list

    Position 1 means the start of the list, so the new node should be the first node.
  2. Step 2: Effect of insertion at position 1

    The new node's next pointer points to the old head, and the old head's previous pointer points back to the new node. The new node becomes the head.
  3. Final Answer:

    The new node becomes the new head of the list. -> Option A
  4. Quick Check:

    Insert at position 1 = new head [OK]
Hint: Position 1 means new head insertion [OK]
Common Mistakes:
  • Thinking position 1 means end insertion
  • Not updating previous pointer of old head
  • Ignoring head pointer update
2. Which of the following is the correct syntax to insert a new node at position pos in a doubly linked list in JavaScript?
easy
A. newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current;
B. newNode.prev = current.next; current.prev = newNode; newNode.next = current; current.next = newNode;
C. newNode.next = current.prev; current.next = newNode; newNode.prev = current.next;
D. newNode.prev = current; current.next = newNode; newNode.next = current.next; current.next.prev = newNode;

Solution

  1. Step 1: Identify correct pointer updates for insertion

    When inserting after current, newNode.next should point to current.next, and current.next.prev should point back to newNode.
  2. Step 2: Update current's next and newNode's prev

    Set current.next to newNode and newNode.prev to current to link newNode properly.
  3. Final Answer:

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

    Correct pointer updates = newNode.next = current.next; current.next.prev = newNode; current.next = newNode; newNode.prev = current; [OK]
Hint: Next and prev pointers must be updated symmetrically [OK]
Common Mistakes:
  • Mixing prev and next pointers
  • Not updating current.next.prev
  • Incorrect order of assignments causing broken links
3. Given the doubly linked list: 10 <-> 20 <-> 30 <-> null, what is the list after inserting 15 at position 2 using correct insertion logic?
medium
A. 10 <-> 20 <-> 30 <-> 15 <-> null
B. 15 <-> 10 <-> 20 <-> 30 <-> null
C. 10 <-> 15 <-> 20 <-> 30 <-> null
D. 10 <-> 20 <-> 15 <-> 30 <-> null

Solution

  1. Step 1: Identify position 2 in the list

    Position 2 means after the first node (10), so 15 should be inserted between 10 and 20.
  2. Step 2: Insert 15 between 10 and 20

    Update pointers so 10 points to 15, 15 points to 20, and 20's prev points to 15.
  3. Final Answer:

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

    Insert at position 2 = between first and second node [OK]
Hint: Position 2 means after first node [OK]
Common Mistakes:
  • Inserting at wrong position
  • Not updating prev pointer of next node
  • Breaking list links after insertion
4. Consider this code snippet to insert a node at position 3 in a doubly linked list:
let current = head;
for(let i = 1; i < pos - 1; i++) {
  current = current.next;
}
newNode.next = current.next;
current.next.prev = newNode;
current.next = newNode;
newNode.prev = current;
What error might occur if pos is greater than the list length?
medium
A. The new node is inserted at the head incorrectly.
B. Runtime error due to accessing next of null.
C. The list becomes circular causing infinite loop.
D. No error; insertion happens at the end automatically.

Solution

  1. Step 1: Analyze loop for position traversal

    The loop moves current forward pos-2 times. If pos is too large, current becomes null.
  2. Step 2: Accessing current.next when current is null

    Trying to access current.next.prev causes runtime error because current is null.
  3. Final Answer:

    Runtime error due to accessing next of null. -> Option B
  4. Quick Check:

    Out-of-range position causes null access error [OK]
Hint: Check list length before insertion [OK]
Common Mistakes:
  • Not validating position against list length
  • Assuming insertion at end without checks
  • Ignoring null pointer exceptions
5. You have a doubly linked list: 5 <-> 10 <-> 15 <-> 20 <-> null. You want to insert a new node with value 12 at position 3. After insertion, you also want to remove the node at position 4. What will be the final list?
hard
A. 5 <-> 12 <-> 10 <-> 20 <-> null
B. 5 <-> 10 <-> 12 <-> 15 <-> 20 <-> null
C. 5 <-> 10 <-> 15 <-> 20 <-> null
D. 5 <-> 10 <-> 12 <-> 20 <-> null

Solution

  1. Step 1: Insert 12 at position 3

    Position 3 means between nodes with values 10 and 15. After insertion: 5 <-> 10 <-> 12 <-> 15 <-> 20.
  2. Step 2: Remove node at position 4

    Position 4 now points to node with value 15. Removing it links 12 directly to 20.
  3. Final Answer:

    5 <-> 10 <-> 12 <-> 20 <-> null -> Option D
  4. Quick Check:

    Insert then remove node at position 4 = 5 <-> 10 <-> 12 <-> 20 <-> null [OK]
Hint: Insert first, then remove target node [OK]
Common Mistakes:
  • Removing wrong node after insertion
  • Not updating pointers after removal
  • Confusing positions after insertion
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