Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Insert at Beginning Head Insert in DSA Typescript - 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 in that order. What is the final state of the list?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

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

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

const list = new LinkedList();
list.insertAtBeginning(10);
list.insertAtBeginning(20);
list.insertAtBeginning(30);
console.log(list.toString());
A20 -> 30 -> 10 -> null
B10 -> 20 -> 30 -> null
C30 -> 20 -> 10 -> null
Dnull
Attempts:
2 left
💡 Hint
Remember, inserting at the beginning means the newest node becomes the head.
🧠 Conceptual
intermediate
1:30remaining
What happens to the head pointer after inserting at the beginning?
When you insert a new node at the beginning of a singly linked list, how does the head pointer change?
AThe head pointer moves to the new node.
BThe head pointer stays the same.
CThe head pointer moves to the last node.
DThe head pointer becomes null.
Attempts:
2 left
💡 Hint
Think about which node is the first after insertion.
🔧 Debug
advanced
2:30remaining
Identify the error in this head insert code snippet
What error will this TypeScript code cause when inserting at the beginning of a linked list? class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtBeginning(value: number) { const newNode = new Node(value); newNode.next = null; this.head = newNode; } }
AThe list will only keep the last inserted node; previous nodes are lost.
BSyntaxError due to missing semicolon.
CTypeError because newNode.next is assigned null.
DNo error; code works correctly.
Attempts:
2 left
💡 Hint
Check how the new node links to the existing list.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple head inserts and one traversal?
Given the following code, what will be printed? class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } class LinkedList { head: Node | null = null; insertAtBeginning(value: number) { 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; } console.log(result + 'null'); } } const list = new LinkedList(); list.insertAtBeginning(5); list.insertAtBeginning(15); list.insertAtBeginning(25); list.printList();
A5 -> 15 -> 25 -> null
B25 -> 15 -> 5 -> null
C15 -> 25 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
New nodes become the head, so the list order reverses insertion order.
🧠 Conceptual
expert
2:30remaining
Why is head insertion efficient in singly linked lists?
Why is inserting a node at the beginning (head) of a singly linked list considered efficient compared to inserting at the end?
ABecause it requires traversing the entire list to find the last node.
BBecause it requires sorting the list after insertion.
CBecause it needs to update all nodes' next pointers.
DBecause it requires updating only the head pointer and the new node's next pointer, both O(1) operations.
Attempts:
2 left
💡 Hint
Think about how many nodes you must visit to insert at the beginning vs end.

Practice

(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Adds a new node at the start of the list
B. Adds a new node at the end of the list
C. Deletes the first node of the list
D. Deletes the last 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 head, pushing the old head to second position.
  3. Final Answer:

    Adds a new node at the start of the list -> Option A
  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 changes the tail node
2. Which of the following is the correct TypeScript syntax to insert a new node at the beginning of a singly linked list?
easy
A. newNode = head; head.next = newNode;
B. head = newNode.next; newNode = head;
C. head.next = newNode; newNode = head;
D. newNode.next = head; 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 D
  4. Quick Check:

    Link new node to head, then update head [OK]
Hint: Set newNode.next to head, then head = newNode [OK]
Common Mistakes:
  • Assigning head before linking newNode.next
  • Swapping assignments order
  • Using head.next instead of newNode.next
3. What will be the linked list after executing the following TypeScript code?
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

let head: Node | null = new Node(10);
let newNode = new Node(5);
newNode.next = head;
head = newNode;
medium
A. 10 -> null
B. 10 -> 5 -> null
C. 5 -> 10 -> null
D. 5 -> null

Solution

  1. Step 1: Initial list setup

    head points to node with value 10, next is null.
  2. Step 2: Insert newNode at beginning

    newNode (5) points to head (10), then head updated to newNode.
  3. Final Answer:

    5 -> 10 -> null -> Option C
  4. Quick Check:

    New node 5 becomes head, points to 10 [OK]
Hint: New node points to old head, then becomes new head [OK]
Common Mistakes:
  • Confusing order of nodes after insertion
  • Forgetting to update head
  • Assuming newNode.next is null
4. Identify the error in this TypeScript code that tries to insert a node at the beginning of a linked list:
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

let head: Node | null = null;
let newNode = new Node(7);
head.next = newNode;
head = newNode;
medium
A. head is null, so head.next causes an error
B. newNode is not linked to head
C. head should be assigned before newNode.next
D. Missing constructor call for newNode

Solution

  1. Step 1: Check initial head value

    head is null, so accessing head.next is invalid and causes runtime error.
  2. Step 2: Correct insertion approach

    Assign newNode.next = head (which is null), then head = newNode.
  3. Final Answer:

    head is null, so head.next causes an error -> Option A
  4. Quick Check:

    Cannot access property on null head [OK]
Hint: Check if head is null before accessing head.next [OK]
Common Mistakes:
  • Trying to access next on null head
  • Not linking newNode.next to head
  • Assigning head after using head.next
5. Given a singly linked list with nodes 20 -> 30 -> 40 -> null, what will be the list after inserting 10 at the beginning using head insert method?
hard
A. 20 -> 30 -> 40 -> 10 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 30 -> 40 -> 10 -> 20 -> null
D. 10 -> 30 -> 40 -> 20 -> null

Solution

  1. Step 1: Understand initial list

    List is 20 -> 30 -> 40 -> null, head points to 20.
  2. Step 2: Insert 10 at beginning

    Create new node 10, set newNode.next = head (20), then head = newNode.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null -> Option B
  4. Quick Check:

    New node 10 becomes head, old list follows [OK]
Hint: New node points to old head, becomes new head [OK]
Common Mistakes:
  • Appending new node at end instead of beginning
  • Mixing node order after insertion
  • Not updating head pointer
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