Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Insert at Beginning Head Insert in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Head Insert Master
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 at the beginning?
Consider a singly linked list initially empty. We insert nodes with values 10, 20, and 30 at the beginning one by one. What is the final state of the list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insertAtBeginning(value) {
    const newNode = new Node(value);
    newNode.next = this.head;
    this.head = newNode;
  }

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

const list = new LinkedList();
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
console.log(list.printList());
Anull -> 30 -> 20 -> 10
B10 -> 20 -> 30 -> null
C30 -> 20 -> 10 -> null
D10 -> null -> 20 -> 30
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
❓ Predict Output
intermediate
2:00remaining
What happens if we insert the same value multiple times at the beginning?
Using the same linked list insertAtBeginning method, insert the value 5 three times. What is the final list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insertAtBeginning(value) {
    const newNode = new Node(value);
    newNode.next = this.head;
    this.head = newNode;
  }

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

const list = new LinkedList();
list.insertAtBeginning(5);
list.insertAtBeginning(5);
list.insertAtBeginning(5);
console.log(list.printList());
Anull -> 5 -> 5 -> 5
B5 -> 5 -> 5 -> null
C5 -> null -> 5 -> 5
D5 -> 5 -> null -> 5
Attempts:
2 left
💡 Hint
Each insertion adds a new node at the front, even if values are the same.
❓ Predict Output
advanced
2:00remaining
What is the output after inserting nodes at the beginning and then printing reversed?
After inserting 1, 2, 3 at the beginning of an empty linked list, we print the list in reverse order using recursion. What is the output?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insertAtBeginning(value) {
    const newNode = new Node(value);
    newNode.next = this.head;
    this.head = newNode;
  }

  printReverse(node = this.head) {
    if (!node) return '';
    return this.printReverse(node.next) + (node.next ? ' -> ' : '') + node.value;
  }
}

const list = new LinkedList();
list.insertAtBeginning(1);
list.insertAtBeginning(2);
list.insertAtBeginning(3);
console.log(list.printReverse());
Anull -> 3 -> 2 -> 1
B3 -> 2 -> 1
C1 -> 3 -> 2
D1 -> 2 -> 3
Attempts:
2 left
💡 Hint
The list is 3 -> 2 -> 1 normally; printing reverse reverses this order.
🧠 Conceptual
advanced
1:30remaining
Why is inserting at the beginning of a singly linked list efficient?
Which reason best explains why inserting a node at the beginning (head) of a singly linked list is efficient?
ABecause it requires updating only the head pointer and the new node's next pointer.
BBecause it requires traversing the entire list to find the last node.
CBecause it requires shifting all existing nodes one position forward.
DBecause it requires creating a new array to hold the nodes.
Attempts:
2 left
💡 Hint
Think about how many nodes you need to touch to insert at the head.
🔧 Debug
expert
2:30remaining
What error occurs when inserting at beginning with missing next assignment?
Consider this insertAtBeginning method missing the line that sets newNode.next = this.head. What error or output results after inserting 1, 2, 3 at the beginning and printing the list?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  insertAtBeginning(value) {
    const newNode = new Node(value);
    // Missing: newNode.next = this.head;
    this.head = newNode;
  }

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

const list = new LinkedList();
list.insertAtBeginning(1);
list.insertAtBeginning(2);
list.insertAtBeginning(3);
console.log(list.printList());
A3 -> null
B3 -> 2 -> 1 -> null
Cnull
DTypeError: Cannot read property 'value' of null
Attempts:
2 left
💡 Hint
Without linking newNode.next to head, what happens to the list?

Practice

(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Deletes the last node of the list
B. Adds a new node at the end of the list
C. Adds a new node at the start of the list
D. Deletes the first node of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means adding a new node before the current first node.
  2. Step 2: Effect on the list

    The new node becomes the new head, pushing the old head to second position.
  3. Final Answer:

    Adds a new node at the start of the list -> Option C
  4. Quick Check:

    Insert at beginning = Add new head [OK]
Hint: Insert at beginning means new head node [OK]
Common Mistakes:
  • Confusing insert at beginning with insert at end
  • Thinking it deletes nodes
  • Assuming it modifies middle nodes
2. Which of the following is the correct syntax to insert a new node at the beginning of a singly linked list in JavaScript?
easy
A. newNode.next = head; head = newNode;
B. head = newNode.next; newNode = head;
C. head.next = newNode; newNode = head;
D. newNode = head.next; head = newNode;

Solution

  1. Step 1: Link new node to current head

    Set newNode.next to point to current head node.
  2. Step 2: Update head to new node

    Assign head to newNode so it becomes the first node.
  3. Final Answer:

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

    Link new node to head, then update head [OK]
Hint: Set newNode.next = head, then head = newNode [OK]
Common Mistakes:
  • Swapping assignments incorrectly
  • Not updating head pointer
  • Assigning head.next instead of newNode.next
3. What will be the output linked list after executing the following code?
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

let head = new Node(10);
let newNode = new Node(20);
newNode.next = head;
head = newNode;

// Print list
let current = head;
let result = [];
while(current) {
  result.push(current.data);
  current = current.next;
}
console.log(result);
medium
A. [20, 10]
B. [10, 20]
C. [10]
D. [20]

Solution

  1. Step 1: Insert newNode at beginning

    newNode (20) points to head (10), then head updated to newNode.
  2. Step 2: Traverse list from new head

    Traversal order: 20 -> 10 -> null, so result array is [20, 10].
  3. Final Answer:

    [20, 10] -> Option A
  4. Quick Check:

    New node at head means list starts with 20 [OK]
Hint: New node points to old head, head updated [OK]
Common Mistakes:
  • Reversing order of nodes in output
  • Ignoring head update
  • Confusing data values
4. Identify the error in the following code snippet that tries to insert a new node at the beginning of a linked list:
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

let head = new Node(5);
let newNode = new Node(15);
head = newNode;
newNode.next = head;
medium
A. The head is not updated to newNode
B. The next pointer of newNode should be null
C. The newNode is not created properly
D. The newNode.next is assigned after head is updated, causing a cycle

Solution

  1. Step 1: Analyze assignment order

    head is set to newNode first, then newNode.next is set to head (which is newNode itself).
  2. Step 2: Detect cycle creation

    This causes newNode.next to point to itself, creating a cycle and breaking the list.
  3. Final Answer:

    The newNode.next is assigned after head is updated, causing a cycle -> Option D
  4. Quick Check:

    Assign next before updating head to avoid cycle [OK]
Hint: Assign newNode.next before head update to avoid cycles [OK]
Common Mistakes:
  • Assigning head before newNode.next
  • Ignoring pointer references
  • Assuming newNode.next should be null always
5. Given a linked list with nodes containing values [30, 20, 10] (head is 30), you want to insert nodes with values [40, 50] at the beginning one by one. What will be the final linked list after these insertions?
hard
A. [40, 50, 30, 20, 10]
B. [50, 40, 30, 20, 10]
C. [30, 20, 10, 40, 50]
D. [10, 20, 30, 40, 50]

Solution

  1. Step 1: Insert 40 at beginning

    New head becomes 40, list is [40, 30, 20, 10].
  2. Step 2: Insert 50 at beginning

    New head becomes 50, list is [50, 40, 30, 20, 10].
  3. Final Answer:

    [50, 40, 30, 20, 10] -> Option B
  4. Quick Check:

    Each insert adds new head, order reverses insert sequence [OK]
Hint: Last inserted node becomes new head, list grows front [OK]
Common Mistakes:
  • Appending instead of prepending
  • Reversing insertion order
  • Not updating head after each insert
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