Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Delete Node at Beginning in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node at Beginning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output after deleting the first node in a singly linked list
What is the printed state of the linked list after deleting the first node?
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtEnd(data: number) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  deleteAtBeginning() {
    if (!this.head) return;
    this.head = this.head.next;
  }

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

const list = new LinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.deleteAtBeginning();
list.printList();
A20 -> 30 -> null
B10 -> 20 -> 30 -> null
C30 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
❓ Predict Output
intermediate
2:00remaining
Output when deleting from an empty linked list
What is the output when deleteAtBeginning is called on an empty linked list?
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  deleteAtBeginning() {
    if (!this.head) return;
    this.head = this.head.next;
  }

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

const list = new LinkedList();
list.deleteAtBeginning();
list.printList();
AEmpty list
Bnull
CError: Cannot read property 'next' of null
Dundefined
Attempts:
2 left
💡 Hint
Deleting from empty list should not change anything.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteAtBeginning method
What error will occur when running this code and why?
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  deleteAtBeginning() {
    this.head = this.head.next;
  }
}

const list = new LinkedList();
list.deleteAtBeginning();
ANo error, deletes first node
BSyntaxError: Unexpected token
CReferenceError: head is not defined
DTypeError: Cannot read property 'next' of null
Attempts:
2 left
💡 Hint
Check what happens if head is null before accessing next.
❓ Predict Output
advanced
2:00remaining
Output after multiple deletions at beginning
What is the printed state of the linked list after deleting the first node twice?
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

  insertAtEnd(data: number) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      return;
    }
    let current = this.head;
    while (current.next) {
      current = current.next;
    }
    current.next = newNode;
  }

  deleteAtBeginning() {
    if (!this.head) return;
    this.head = this.head.next;
  }

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

const list = new LinkedList();
list.insertAtEnd(5);
list.insertAtEnd(15);
list.insertAtEnd(25);
list.deleteAtBeginning();
list.deleteAtBeginning();
list.printList();
A5 -> 15 -> 25 -> null
B15 -> 25 -> null
C25 -> null
Dnull
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
🧠 Conceptual
expert
2:00remaining
Effect of deleting the first node on list length
If a singly linked list has 7 nodes, what will be the length after deleting the first node?
A6
B7
C5
D0
Attempts:
2 left
💡 Hint
Deleting the first node removes exactly one node from the list.

Practice

(1/5)
1. What happens when you delete the first node in a singly linked list?
easy
A. The head pointer moves to the second node
B. The last node is deleted
C. The list becomes empty immediately
D. The middle node is removed

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the list.
  2. Step 2: Delete the first node by moving head

    Deleting the first node means moving the head pointer to the next node.
  3. Final Answer:

    The head pointer moves to the second node -> Option A
  4. Quick Check:

    Delete first node = move head to next [OK]
Hint: Deleting first node means moving head to next node [OK]
Common Mistakes:
  • Thinking the last node is deleted
  • Assuming list becomes empty always
  • Deleting a middle node instead
2. Which of the following is the correct TypeScript syntax to delete the first node in a singly linked list with head pointer head?
easy
A. head = head.next;
B. head.next = head;
C. delete head;
D. head = null.next;

Solution

  1. Step 1: Understand head and next pointers

    head points to first node; head.next points to second node.
  2. Step 2: Assign head to head.next to delete first node

    Setting head = head.next moves head to second node, effectively deleting first.
  3. Final Answer:

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

    Correct syntax to delete first node = head = head.next; [OK]
Hint: Use head = head.next to delete first node [OK]
Common Mistakes:
  • Using delete keyword incorrectly
  • Assigning head.next to head
  • Accessing null.next causing error
3. Given the linked list: 1 -> 2 -> 3 -> null and the code snippet:
head = head.next;

What is the resulting linked list?
medium
A. 1 -> 2 -> 3 -> null
B. 2 -> 3 -> null
C. 3 -> null
D. null

Solution

  1. Step 1: Initial list and head pointer

    Head points to node with value 1, list is 1 -> 2 -> 3 -> null.
  2. Step 2: Move head to head.next

    head = head.next moves head to node with value 2, removing first node.
  3. Final Answer:

    2 -> 3 -> null -> Option B
  4. Quick Check:

    After deletion, list starts at 2 [OK]
Hint: After head = head.next, list starts from second node [OK]
Common Mistakes:
  • Assuming list remains unchanged
  • Thinking last node is deleted
  • Confusing node values after deletion
4. Consider this TypeScript code to delete the first node:
if (head !== null) {
  head = head.next;
}

What error does this fix compared to just head = head.next;?
medium
A. Prevents deleting the last node
B. Prevents memory leak
C. Prevents runtime error if list is empty
D. Prevents deleting middle node

Solution

  1. Step 1: Understand head could be null

    If list is empty, head is null and head.next causes error.
  2. Step 2: Check head before accessing next

    Condition ensures head is not null before moving to head.next, avoiding runtime error.
  3. Final Answer:

    Prevents runtime error if list is empty -> Option C
  4. Quick Check:

    Check head != null before head.next avoids error [OK]
Hint: Check head not null before deleting first node [OK]
Common Mistakes:
  • Ignoring null head causing crash
  • Thinking it prevents memory leaks
  • Confusing node positions deleted
5. You have a singly linked list with nodes: 10 -> 20 -> 30 -> 40 -> null. You want to delete the first two nodes safely. Which code snippet correctly does this?
hard
A. head = null;
B. head = head.next; head = head.next;
C. head.next = head.next.next;
D. if (head && head.next) { head = head.next.next; }

Solution

  1. Step 1: Understand deleting two nodes means moving head twice

    Deleting first two nodes means head should point to third node.
  2. Step 2: Check head and head.next exist before moving head twice

    Condition ensures no runtime error when accessing head.next.next.
  3. Final Answer:

    if (head && head.next) { head = head.next.next; } -> Option D
  4. Quick Check:

    Check head and head.next before double move [OK]
Hint: Check head and head.next before skipping two nodes [OK]
Common Mistakes:
  • Not checking head.next causing error
  • Assigning head.next instead of head
  • Setting head to null deletes entire list
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