Bird
Raised Fist0
DSA Javascriptprogramming~5 mins

Insert at Beginning of Doubly Linked List in DSA Javascript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a doubly linked list?
A doubly linked list is a chain of nodes where each node has three parts: data, a pointer to the next node, and a pointer to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What steps are needed to insert a new node at the beginning of a doubly linked list?
1. Create a new node with the given data.
2. Set the new node's next pointer to the current head.
3. Set the new node's previous pointer to null.
4. If the list is not empty, set the current head's previous pointer to the new node.
5. Update the head to be the new node.
Click to reveal answer
beginner
Why do we need to update the previous pointer of the old head when inserting at the beginning?
Because the old head now comes after the new node, its previous pointer must point back to the new node to maintain the doubly linked structure.
Click to reveal answer
beginner
What happens if the doubly linked list is empty when inserting at the beginning?
The new node becomes the head of the list, and both its next and previous pointers are null because it is the only node.
Click to reveal answer
beginner
Show the state of the list after inserting 10 at the beginning of an empty doubly linked list.
List: 10 -> null
Previous pointers: null <- 10
Click to reveal answer
What pointer of the new node should be set to null when inserting at the beginning?
ABoth pointers
BNext pointer
CNeither pointer
DPrevious pointer
After inserting a new node at the beginning, what should the head pointer point to?
AThe new node
BThe old head node
CNull
DThe last node
If the list is empty, what is the previous pointer of the new node after insertion?
APoints to null
BPoints to itself
CPoints to old head
DUndefined
Which pointer of the old head node must be updated when inserting a new node at the beginning?
ANext pointer
BNo pointer
CPrevious pointer
DBoth pointers
What is the time complexity of inserting at the beginning of a doubly linked list?
AO(log n)
BO(1)
CO(n)
DO(n^2)
Explain how to insert a new node at the beginning of a doubly linked list step-by-step.
Think about how pointers change when adding a new first node.
You got /5 concepts.
    Describe the state of the doubly linked list after inserting nodes 5, then 10 at the beginning.
    Visualize the list as 10 <-> 5
    You got /5 concepts.

      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