Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

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

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

class LinkedList {
  head: Node | null = null;

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

  deleteAtEnd() {
    if (!this.head) return;
    if (!this.head.next) {
      this.head = null;
      return;
    }
    let current = this.head;
    while (current.next && current.next.next) {
      current = current.next;
    }
    current.next = null;
  }

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

const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.deleteAtEnd();
list.printList();
A10 -> 20 -> 30 -> null
B10 -> 20 -> null
C20 -> 30 -> null
D10 -> null
Attempts:
2 left
💡 Hint
Think about what happens to the last node when you delete it.
❓ Predict Output
intermediate
2:00remaining
Output after deleting the last node from a single-node list
What is the printed state of the linked list after deleting the last node from a list with one node?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

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

  deleteAtEnd() {
    if (!this.head) return;
    if (!this.head.next) {
      this.head = null;
      return;
    }
    let current = this.head;
    while (current.next && current.next.next) {
      current = current.next;
    }
    current.next = null;
  }

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

const list = new LinkedList();
list.append(100);
list.deleteAtEnd();
list.printList();
AError: Cannot delete from empty list
B100 -> null
Cnull
D100
Attempts:
2 left
💡 Hint
Deleting the only node should leave the list empty.
🔧 Debug
advanced
2:00remaining
Identify the error in deleteAtEnd method for a single-node list
What error will this code produce when deleting the last node from a list with one node?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  head: Node | null = null;

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

  deleteAtEnd() {
    if (!this.head) return;
    let current = this.head;
    while (current.next && current.next.next) {
      current = current.next;
    }
    current.next = null;
  }

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

const list = new LinkedList();
list.append(1);
list.deleteAtEnd();
list.printList();
ASyntaxError: Unexpected token
BNo error, prints '1 -> null'
CRuntimeError: Infinite loop
DTypeError: Cannot read property 'next' of null
Attempts:
2 left
💡 Hint
Check the while loop condition when list has one node.
❓ Predict Output
advanced
2:00remaining
Output after deleting last node in a circular linked list
What is the printed state of the circular linked list after deleting the last node?
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

class CircularLinkedList {
  head: Node | null = null;

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

  deleteAtEnd() {
    if (!this.head) return;
    if (this.head.next === this.head) {
      this.head = null;
      return;
    }
    let current = this.head;
    while (current.next && current.next.next !== this.head) {
      current = current.next;
    }
    current.next = this.head;
  }

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

const list = new CircularLinkedList();
list.append(5);
list.append(10);
list.append(15);
list.deleteAtEnd();
list.printList();
A5 -> 10 -> (head)
B5 -> 10 -> 15 -> (head)
C10 -> 15 -> (head)
D5 -> (head)
Attempts:
2 left
💡 Hint
Deleting last node means the node before head points to head.
🧠 Conceptual
expert
2:00remaining
Why is deleting the last node in a singly linked list less efficient than deleting the first node?
Choose the best explanation for why deleting the last node in a singly linked list is slower than deleting the first node.
ABecause to delete the last node, you must traverse the entire list to find the node before last, while deleting the first node only requires updating the head pointer.
BBecause deleting the last node requires shifting all nodes forward, but deleting the first node does not.
CBecause singly linked lists store nodes in reverse order, so deleting last node is harder.
DBecause deleting the last node requires copying all node values to a new list, but deleting the first node does not.
Attempts:
2 left
💡 Hint
Think about what pointers you need to update for each deletion.

Practice

(1/5)
1. What does the operation Delete Node at End do in a singly linked list?
easy
A. Adds a new node at the beginning of the list
B. Removes the first node from the list
C. Removes the last node from the list
D. Adds a new node at the end of the list

Solution

  1. Step 1: Understand the operation

    Deleting a node at the end means removing the last node in the linked list.
  2. Step 2: Identify the effect on the list

    After removal, the second last node becomes the new last node.
  3. Final Answer:

    Removes the last node from the list -> Option C
  4. Quick Check:

    Delete Node at End = Remove last node [OK]
Hint: Delete last node means remove tail node [OK]
Common Mistakes:
  • Confusing delete at end with delete at start
  • Thinking it adds a node instead of removing
  • Assuming it deletes a middle node
2. Which of the following TypeScript code snippets correctly deletes the last node of a singly linked list named head?
easy
A. if (!head) return null; head = head.next;
B. if (!head) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null;
C. let temp = head; while (temp.next) { temp = temp.next; } temp = null;
D. head = null;

Solution

  1. Step 1: Analyze code for deleting last node

    if (!head) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; traverses to the second last node and sets its next to null, removing the last node.
  2. Step 2: Check other options

    if (!head) return null; head = head.next; deletes the first node, C tries to set temp to null but does not affect list, D deletes entire list.
  3. Final Answer:

    Traverse to second last node, set next to null -> Option B
  4. Quick Check:

    Traverse to second last node, set next to null = A [OK]
Hint: Stop at second last node, set next to null [OK]
Common Mistakes:
  • Setting temp to null does not remove node from list
  • Deleting head instead of last node
  • Not checking for empty or single-node list
3. Given the linked list: 1 -> 2 -> 3 -> 4 -> null, what will be the list after deleting the node at the end using this code?
function deleteAtEnd(head: Node | null): Node | null {
  if (!head) return null;
  if (!head.next) return null;
  let temp = head;
  while (temp.next && temp.next.next) {
    temp = temp.next;
  }
  temp.next = null;
  return head;
}
medium
A. 2 -> 3 -> 4 -> null
B. 1 -> 2 -> 3 -> 4 -> null
C. null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand the code logic

    The code removes the last node by stopping at second last node and setting its next to null.
  2. Step 2: Apply to given list

    Original list ends with 4. After deletion, 3's next becomes null, so list is 1 -> 2 -> 3 -> null.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option D
  4. Quick Check:

    Last node removed, list ends at 3 [OK]
Hint: Last node removed, second last points to null [OK]
Common Mistakes:
  • Not updating second last node's next to null
  • Returning null for multi-node list
  • Deleting first node instead of last
4. Identify the bug in this TypeScript function that deletes the last node of a singly linked list:
function deleteAtEnd(head: Node | null): Node | null {
  if (!head) return null;
  let temp = head;
  while (temp.next) {
    temp = temp.next;
  }
  temp = null;
  return head;
}
medium
A. It does not update the second last node's next pointer
B. It does not handle empty list
C. It deletes the head node instead of last
D. It causes infinite loop

Solution

  1. Step 1: Analyze the loop

    The loop moves temp to the last node, but does not track second last node.
  2. Step 2: Check deletion step

    Setting temp = null only changes local variable, does not remove last node from list.
  3. Final Answer:

    It does not update the second last node's next pointer -> Option A
  4. Quick Check:

    Must update second last node's next to null [OK]
Hint: Update second last node's next to null, not temp variable [OK]
Common Mistakes:
  • Setting temp to null does not remove node
  • Not tracking second last node in loop
  • Ignoring single-node list case
5. You have a singly linked list with nodes: 10 -> 20 -> 30 -> 40 -> null. Write a TypeScript function to delete the last node and return the updated list. Which of the following implementations correctly handles the edge case when the list has only one node?
hard
A. function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; if (!head.next) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; return head; }
B. function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; let temp = head; while (temp.next) { temp = temp.next; } temp = null; return head; }
C. function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; if (!head.next) return head; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; return head; }
D. function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; if (!head.next) return head; let temp = head; while (temp.next) { temp = temp.next; } temp.next = null; return head; }

Solution

  1. Step 1: Check handling of single-node list

    function deleteAtEnd(head: Node | null): Node | null { if (!head) return null; if (!head.next) return null; let temp = head; while (temp.next && temp.next.next) { temp = temp.next; } temp.next = null; return head; } returns null if only one node exists, correctly deleting it.
  2. Step 2: Verify deletion of last node for longer lists

    For longer lists, it traverses to the second last node using while (temp.next && temp.next.next) and sets temp.next = null, correctly removing the last node.
  3. Final Answer:

    Correctly handles single-node by returning null -> Option A
  4. Quick Check:

    Return null for single node, else remove last node [OK]
Hint: Return null if single node, else remove last node [OK]
Common Mistakes:
  • Not returning null for single-node list
  • Setting temp to null instead of updating next pointer
  • Incorrect loop condition causing errors
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