Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Delete Node at End in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Delete Node at End
Start at Head
↓
Is list empty?
Yes→Stop, nothing to delete
No↓
Is only one node?
Yes→Delete head, list empty
No↓
Traverse to second last node
↓
Set second last node's next to null
↓
Node at end deleted
↓
Stop
Start from the head, check if list is empty or has one node, else traverse to second last node and remove last node by setting next to null.
Execution Sample
DSA Typescript
class Node {
  constructor(public data: number, public next: Node | null = null) {}
}

function deleteNodeAtEnd(head: Node | null): Node | null {
  if (!head) return null;
  if (!head.next) return null;
  let current = head;
  while (current.next && current.next.next) {
    current = current.next;
  }
  current.next = null;
  return head;
}
Deletes the last node from a singly linked list and returns the updated head.
Execution Table
StepActionCurrent Node DataNext Node DataList State
1Check if head is null121 -> 2 -> 3 -> null
2Check if head.next is null121 -> 2 -> 3 -> null
3Set current = head121 -> 2 -> 3 -> null
4Check current.next and current.next.next121 -> 2 -> 3 -> null
5Move current to current.next231 -> 2 -> 3 -> null
6Check current.next and current.next.next231 -> 2 -> 3 -> null
7current.next.next is null, stop loop231 -> 2 -> 3 -> null
8Set current.next = null (delete last node)2null1 -> 2 -> null
9Return head2null1 -> 2 -> null
10End2null1 -> 2 -> null
💡 Loop stops when current.next.next is null, meaning current is second last node.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 8Final
headNode(1)Node(1)Node(1)Node(1)Node(1)
currentnullNode(1)Node(2)Node(2)Node(2)
Key Moments - 3 Insights
Why do we check if head.next is null before traversing?
Because if head.next is null, it means there is only one node. We delete it by returning null. This is shown in execution_table row 2.
Why do we stop the loop when current.next.next is null?
Because current.next.next being null means current.next is the last node. We want current to be the second last node to remove the last node. See execution_table rows 6 and 7.
What happens if the list is empty at the start?
If head is null, we return null immediately, meaning nothing to delete. This is shown in execution_table row 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'current' after step 5?
ANode with data 1
BNode with data 3
CNode with data 2
Dnull
💡 Hint
Check the 'Current Node Data' column at step 5 in execution_table.
At which step does the loop stop traversing the list?
AStep 7
BStep 5
CStep 4
DStep 8
💡 Hint
Look for the step where current.next.next is null and loop ends in execution_table.
If the list had only one node, what would the function return?
AThe same single node
Bnull
CA new node
DAn error
💡 Hint
Refer to execution_table row 2 where head.next is null case is handled.
Concept Snapshot
Delete Node at End in Singly Linked List:
- Check if list empty (head null) -> return null
- Check if only one node (head.next null) -> return null
- Traverse to second last node (current.next.next != null)
- Set current.next = null to remove last node
- Return head as updated list
Full Transcript
This concept shows how to delete the last node from a singly linked list. We start by checking if the list is empty or has only one node. If empty or single node, we return null because the list becomes empty after deletion. Otherwise, we traverse the list to find the second last node by checking current.next.next. When we find it, we set current.next to null, removing the last node. Finally, we return the head of the updated list. The execution table traces each step, showing how current moves and when the loop stops. The variable tracker shows how current and head change during execution. Key moments clarify why we check certain conditions and when we stop traversing. The visual quiz tests understanding of these steps.

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