Bird
Raised Fist0
DSA Typescriptprogramming

Pop Using Linked List Node in DSA Typescript

Choose your learning style10 modes available
Mental Model
Pop removes the last item from a linked list by finding the second last node and cutting off the last node.
Analogy: Imagine a chain of paper clips linked together. To remove the last clip, you hold the second last clip and unhook the last one.
head -> 1 -> 2 -> 3 -> null
           ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, pop last node
Goal: Remove the last node (3) and update the list to end at node 2
Step 1: start at head node (1)
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin traversal from the first node to find the last node
Step 2: move curr to next node (2)
head -> 1 -> [curr->2] -> 3 -> null
Why: We move forward to find the node before the last
Step 3: check next node of curr (3) which is last node
head -> 1 -> [curr->2] -> 3 -> null
Why: Curr is now at second last node, ready to remove last node
Step 4: set curr.next to null, removing last node (3)
head -> 1 -> 2 -> null
Why: Disconnect last node to pop it from the list
Result:
head -> 1 -> 2 -> null
Annotated Code
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

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

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

  pop(): number | null {
    if (!this.head) return null; // empty list
    if (!this.head.next) {
      const val = this.head.value;
      this.head = null;
      return val;
    }
    let curr = this.head;
    while (curr.next && curr.next.next) {
      curr = curr.next; // advance to second last node
    }
    const val = curr.next!.value; // last node value
    curr.next = null; // remove last node
    return val;
  }

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

// Driver code
const list = new LinkedList();
list.push(1);
list.push(2);
list.push(3);
console.log('Before pop:', list.printList());
const poppedValue = list.pop();
console.log('Popped value:', poppedValue);
console.log('After pop:', list.printList());
if (!this.head) return null; // empty list
handle empty list edge case
if (!this.head.next) { const val = this.head.value; this.head = null; return val; }
handle single node list by removing head
while (curr.next && curr.next.next) { curr = curr.next; }
advance curr to second last node
const val = curr.next!.value; curr.next = null;
store last node value and remove it by setting next to null
OutputSuccess
Before pop: 1 -> 2 -> 3 -> null Popped value: 3 After pop: 1 -> 2 -> null
Complexity Analysis
Time: O(n) because we traverse the list once to find the second last node
Space: O(1) because we only use a few pointers regardless of list size
vs Alternative: Compared to arrays where pop is O(1), linked list pop requires traversal making it O(n)
Edge Cases
empty list
pop returns null without error
DSA Typescript
if (!this.head) return null; // empty list
single node list
pop removes the only node and sets head to null
DSA Typescript
if (!this.head.next) { const val = this.head.value; this.head = null; return val; }
When to Use This Pattern
When you need to remove the last element from a singly linked list, use the pop pattern by traversing to the second last node and cutting off the last.
Common Mistakes
Mistake: Trying to remove last node without checking if list is empty or has one node
Fix: Add checks for empty list and single node list before traversal
Mistake: Stopping traversal at last node instead of second last node
Fix: Traverse until curr.next.next is null to reach second last node
Summary
Removes the last node from a singly linked list by updating the second last node's next pointer to null.
Use when you want to delete the last element from a linked list.
The key is to stop at the second last node so you can remove the last node safely.

Practice

(1/5)
1. What does the pop() operation do in a linked list when implemented using nodes?
easy
A. Removes the last node and returns its value
B. Adds a new node at the beginning
C. Removes the first node and returns its value
D. Searches for a node with a specific value

Solution

  1. Step 1: Understand pop operation in linked list context

    Pop means removing the last element from the list, similar to popping from a stack.
  2. Step 2: Identify what pop returns

    Pop removes the last node and returns its stored value.
  3. Final Answer:

    Removes the last node and returns its value -> Option A
  4. Quick Check:

    Pop = remove last node [OK]
Hint: Pop removes last node and returns its value [OK]
Common Mistakes:
  • Confusing pop with push operation
  • Thinking pop removes the first node
  • Assuming pop searches for a value
2. Which of the following is the correct TypeScript syntax to define a node class for a singly linked list?
easy
A. class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } }
B. class Node { val: number; next: Node; constructor(val: number) { this.val = val; this.next = null; } }
C. class Node { value: number; next: Node; constructor(value: number) { this.value = value; this.next = undefined; } }
D. class Node { value: number; next: Node | undefined; constructor(value: number) { this.value = value; this.next = null; } }

Solution

  1. Step 1: Check property types for linked list node

    Node should have a value and a next pointer which can be null if no next node.
  2. Step 2: Verify constructor initialization

    Constructor sets value and initializes next to null (no next node initially).
  3. Final Answer:

    class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option A
  4. Quick Check:

    Node next can be null [OK]
Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
  • Not allowing next to be null
  • Using undefined instead of null
  • Mismatching property names
3. Given the following linked list code, what will be the printed output after calling pop() once?
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;
  }

  pop() {
    if (!this.head) return null;
    if (!this.head.next) {
      const val = this.head.value;
      this.head = null;
      return val;
    }
    let current = this.head;
    while (current.next && current.next.next) {
      current = current.next;
    }
    const val = current.next!.value;
    current.next = null;
    return val;
  }

  print() {
    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.pop();
list.print();
medium
A. null
B. 10 -> 20 -> null
C. 20 -> 30 -> null
D. 10 -> 20 -> 30 -> null

Solution

  1. Step 1: Understand list before pop

    List after appending 10, 20, 30 is: 10 -> 20 -> 30 -> null.
  2. Step 2: Trace pop operation

    Pop removes last node (30), so list becomes 10 -> 20 -> null.
  3. Final Answer:

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

    Pop removes last node [OK]
Hint: Pop removes last node, print shows remaining [OK]
Common Mistakes:
  • Assuming pop removes first node
  • Not updating next pointer to null
  • Printing old list without pop effect
4. Identify the bug in the following pop() method for a singly linked list and select the correct fix.
pop() {
  if (!this.head) return null;
  let current = this.head;
  while (current.next) {
    current = current.next;
  }
  const val = current.value;
  current = null;
  return val;
}
medium
A. Return current.next.value instead of current.value
B. Initialize current as null before the loop
C. The loop should stop at the second last node; set current.next = null to remove last node
D. No bug; code works correctly

Solution

  1. Step 1: Analyze loop behavior

    The loop moves current to the last node, but we need to stop at second last to remove last node.
  2. Step 2: Fix removal of last node

    Set current.next = null at second last node to remove last node properly.
  3. Final Answer:

    The loop should stop at the second last node; set current.next = null to remove last node -> Option C
  4. Quick Check:

    Stop at second last node to remove last [OK]
Hint: Stop at second last node to remove last [OK]
Common Mistakes:
  • Setting current = null does not remove node
  • Loop goes to last node instead of second last
  • Returning wrong node value
5. You have a singly linked list with nodes: 5 -> 15 -> 25 -> 35 -> null. You want to implement a pop() method that removes the last node and returns its value. However, if the list has only one node, pop() should remove it and set the list to empty. What will be the state of the list after calling pop() twice?
Choose the correct printed output after these operations:
const list = new LinkedList();
list.append(5);
list.append(15);
list.append(25);
list.append(35);
list.pop();
list.pop();
list.print();
hard
A. null
B. 5 -> 15 -> 25 -> null
C. 25 -> 35 -> null
D. 5 -> 15 -> null

Solution

  1. Step 1: Initial list and first pop

    List starts as 5 -> 15 -> 25 -> 35 -> null. First pop removes 35, list becomes 5 -> 15 -> 25 -> null.
  2. Step 2: Second pop removes last node 25

    After second pop, list is 5 -> 15 -> null.
  3. Final Answer:

    5 -> 15 -> null -> Option D
  4. Quick Check:

    Two pops remove last two nodes [OK]
Hint: Each pop removes last node, so two pops remove last two nodes [OK]
Common Mistakes:
  • Removing nodes from front instead of end
  • Not updating list head when one node left
  • Assuming pop removes all nodes
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