Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Delete Node 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 deleting the first node?
Consider a singly linked list with nodes containing values 10 -> 20 -> 30 -> null. After deleting the node at the beginning, what will be the printed list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    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.append(10);
list.append(20);
list.append(30);
list.deleteAtBeginning();
list.printList();
A30 -> null
B20 -> 30 -> null
C10 -> 20 -> 30 -> null
Dnull
Attempts:
2 left
💡 Hint
Deleting the first node means the head moves to the next node.
❓ Predict Output
intermediate
2:00remaining
What happens when deleting from an empty list?
Given an empty singly linked list, what will be the output after calling deleteAtBeginning() and then printing the list?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = 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
Bundefined
CError: Cannot read property 'next' of null
Dnull
Attempts:
2 left
💡 Hint
Deleting from empty list should not change anything.
❓ Predict Output
advanced
2:00remaining
What is the output after multiple deletions at beginning?
Starting with a linked list 5 -> 15 -> 25 -> 35 -> null, after calling deleteAtBeginning() twice and then printing, what is the output?
DSA Javascript
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

  append(data) {
    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.append(5);
list.append(15);
list.append(25);
list.append(35);
list.deleteAtBeginning();
list.deleteAtBeginning();
list.printList();
A25 -> 35 -> null
B15 -> 25 -> 35 -> null
C35 -> null
D5 -> 15 -> 25 -> 35 -> null
Attempts:
2 left
💡 Hint
Each deletion removes the current head node.
🧠 Conceptual
advanced
1:00remaining
What is the time complexity of deleting the first node in a singly linked list?
Consider a singly linked list with n nodes. What is the time complexity of deleting the node at the beginning?
AO(log n) - Logarithmic time
BO(n) - Linear time
CO(1) - Constant time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Deleting the first node only changes the head pointer.
🔧 Debug
expert
2:00remaining
Identify the error in this deleteAtBeginning method
What error will this code produce when deleteAtBeginning is called on a non-empty list? class LinkedList { constructor() { this.head = null; } deleteAtBeginning() { this.head = this.head.next.next; } }
ATypeError: Cannot read property 'next' of null
BNo error, deletes first node correctly
CSyntaxError: Unexpected token
DReferenceError: head is not defined
Attempts:
2 left
💡 Hint
Check what happens if head.next is null.

Practice

(1/5)
1. What happens to the head pointer when you delete the first node in a singly linked list?
easy
A. It moves to the next node in the list
B. It becomes null immediately
C. It points to the last node
D. It remains unchanged

Solution

  1. Step 1: Understand the head pointer role

    The head pointer points to the first node in the list.
  2. Step 2: Effect of deleting the first node

    Deleting the first node means the head must now point to the second node, which becomes the new first node.
  3. Final Answer:

    It moves to the next node in the list -> Option A
  4. Quick Check:

    Head points to next node after deletion [OK]
Hint: Head always moves to next node when deleting first [OK]
Common Mistakes:
  • Assuming head becomes null even if list has more nodes
  • Thinking head points to last node after deletion
  • Not updating head pointer at all
2. Which of the following is the correct syntax to delete the first node in a singly linked list in JavaScript?
easy
A. delete head;
B. head.next = head;
C. head = head.next;
D. head = null.next;

Solution

  1. Step 1: Understand how to move head pointer

    To delete the first node, assign head to head.next to skip the first node.
  2. Step 2: Check each option

    head = head.next; correctly assigns head to head.next. head.next = head; reverses pointers incorrectly. delete head; deletes the variable but not the node. head = null.next; is invalid syntax.
  3. Final Answer:

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

    Assign head to head.next to delete first node [OK]
Hint: Use head = head.next to remove first node [OK]
Common Mistakes:
  • Using delete keyword incorrectly
  • Assigning head.next to head instead of head to head.next
  • Using invalid syntax like null.next
3. Consider the following linked list code in JavaScript:
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

// Delete first node
head = head.next;

// Print list
let current = head;
let result = [];
while(current) {
  result.push(current.data);
  current = current.next;
}
console.log(result);

What will be printed?
medium
A. []
B. [10, 20, 30]
C. [30]
D. [20, 30]

Solution

  1. Step 1: Initial linked list nodes

    The list has nodes with data 10 -> 20 -> 30.
  2. Step 2: Deleting first node by head = head.next

    Head moves from node 10 to node 20, so node 10 is removed.
  3. Step 3: Printing remaining nodes

    Starting from new head (20), the list is 20 -> 30, so result array is [20, 30].
  4. Final Answer:

    [20, 30] -> Option D
  5. Quick Check:

    After deletion, list starts at 20 [OK]
Hint: Deleting first node skips first data in output [OK]
Common Mistakes:
  • Printing original list without deletion
  • Confusing head.next with head
  • Forgetting to update head pointer
4. The following code attempts to delete the first node of a linked list but causes an error:
head.next = head.next.next;

What is the problem with this code if head points to the first node?
medium
A. It deletes the second node instead of the first node
B. It causes a runtime error if head.next is null
C. It correctly deletes the first node
D. It sets head to null incorrectly

Solution

  1. Step 1: Analyze what head.next = head.next.next does

    This changes the next pointer of the first node to skip the second node, effectively deleting the second node.
  2. Step 2: Check if first node is deleted

    The head pointer still points to the first node, so the first node is not deleted.
  3. Final Answer:

    It deletes the second node instead of the first node -> Option A
  4. Quick Check:

    head.next changes second node, not head itself [OK]
Hint: Changing head.next skips second node, not first [OK]
Common Mistakes:
  • Thinking this deletes the first node
  • Ignoring possible null pointer if list has one node
  • Confusing head and head.next pointers
5. You have a singly linked list with nodes: 5 -> 10 -> 15 -> 20. Write a function in JavaScript to delete the first node and return the new head. What will be the list after deletion?
hard
A. 20
B. 10 -> 15 -> 20
C. 15 -> 20
D. 5 -> 10 -> 15 -> 20

Solution

  1. Step 1: Write function to delete first node

    Function returns head.next to skip first node:
    function deleteFirstNode(head) { return head ? head.next : null; }
  2. Step 2: Apply function to list 5 -> 10 -> 15 -> 20

    New head points to node with data 10, so list becomes 10 -> 15 -> 20.
  3. Final Answer:

    10 -> 15 -> 20 -> Option B
  4. Quick Check:

    Deleting first node moves head to second node [OK]
Hint: Return head.next to delete first node [OK]
Common Mistakes:
  • Returning head instead of head.next
  • Not handling empty list (null head)
  • Deleting wrong node by changing next pointers incorrectly
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