Bird
Raised Fist0
DSA Javascriptprogramming

Delete Node at End in DSA Javascript

Choose your learning style10 modes available
Mental Model
To remove the last item, find the one before it and make it the new end by cutting off the last.
Analogy: Imagine a line of people holding hands. To remove the last person, the second last person lets go of their hand, so the last person leaves the line.
head -> 1 -> 2 -> 3 -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, delete node at end
Goal: Remove the last node so the list ends at 2
Step 1: start at head node (1)
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin from the start to find the node before the last
Step 2: move curr to node 2, check next node
head -> 1 -> [curr->2] -> 3 -> null
Why: We want to stop at the node before the last node
Step 3: curr.next points to last node (3), so set curr.next to null to remove last node
head -> 1 -> 2 -> null
Why: Cutting off the last node removes it from the list
Result:
head -> 1 -> 2 -> null
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

  append(value) {
    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;
  }

  deleteAtEnd() {
    if (!this.head) return; // empty list
    if (!this.head.next) {
      this.head = null; // single node
      return;
    }
    let curr = this.head;
    while (curr.next.next) {
      curr = curr.next; // advance to node before last
    }
    curr.next = null; // remove last node
  }

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

// Driver code
const list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
list.printList();
list.deleteAtEnd();
list.printList();
if (!this.head) return; // empty list
handle empty list by returning early
if (!this.head.next) { this.head = null; // single node return; }
handle single node list by setting head to null
while (curr.next.next) { curr = curr.next; // advance to node before last }
move curr to the node before the last node
curr.next = null; // remove last node
cut off last node by setting next to null
OutputSuccess
1 -> 2 -> 3 -> null 1 -> 2 -> null
Complexity Analysis
Time: O(n) because we traverse the list once to find the node before the last
Space: O(1) because we only use a few pointers regardless of list size
vs Alternative: Compared to removing from the front which is O(1), deleting at end requires traversal making it O(n)
Edge Cases
empty list
function returns immediately without error
DSA Javascript
if (!this.head) return; // empty list
single node list
head is set to null, list becomes empty
DSA Javascript
if (!this.head.next) {
  this.head = null; // single node
  return;
}
When to Use This Pattern
When asked to remove the last item from a singly linked list, use the pattern of finding the node before last and cutting its next pointer.
Common Mistakes
Mistake: Trying to delete last node by setting head to null without checking list size
Fix: Check if list has only one node and handle separately by setting head to null
Mistake: Not stopping at node before last, causing null pointer errors
Fix: Use condition curr.next.next to stop at node before last
Summary
Deletes the last node from a singly linked list by cutting off the last pointer.
Use when you need to remove the end element of a linked list.
The key is to stop at the node before the last and set its next pointer to null.

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 end of the linked list
B. Removes the first node from the linked list
C. Removes the last node from the linked list
D. Adds a new node at the beginning of the linked list

Solution

  1. Step 1: Understand the linked list structure

    A singly linked list is a chain of nodes where each node points to the next node, ending with a node pointing to null.
  2. Step 2: Identify what deleting the last node means

    Deleting the last node means removing the node that points to null, effectively shortening the list by one node at the end.
  3. Final Answer:

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

    Delete Node at End = Remove last node [OK]
Hint: Last node points to null; delete it to remove end [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 is the correct syntax to delete the last node in a singly linked list in JavaScript?
easy
A. list.deleteEnd();
B. list.pop();
C. list.deleteNodeAtEnd();
D. list.removeLastNode();

Solution

  1. Step 1: Recognize common JavaScript linked list methods

    JavaScript arrays use pop() to remove the last element; custom linked lists often implement similar methods.
  2. Step 2: Identify the standard method name

    Among the options, pop() is the standard method name used to remove the last element in many list implementations.
  3. Final Answer:

    list.pop(); -> Option B
  4. Quick Check:

    pop() removes last element [OK]
Hint: pop() usually removes last element in lists [OK]
Common Mistakes:
  • Using non-standard method names
  • Confusing delete with remove
  • Assuming deleteEnd() is built-in
3. Consider the following JavaScript code for a singly linked list deletion at end:
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

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

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

let list = new LinkedList();
list.head = new Node(1);
list.head.next = new Node(2);
list.head.next.next = new Node(3);
list.deleteAtEnd();

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

What will be printed?
medium
A. [1, 2, 3]
B. [2, 3]
C. [1]
D. [1, 2]

Solution

  1. Step 1: Understand the initial list and deletion

    The list starts as 1 -> 2 -> 3. The deleteAtEnd() method removes the last node (3).
  2. Step 2: Trace the deletion and final list

    After deletion, the list becomes 1 -> 2. The loop collects these values into the array [1, 2].
  3. Final Answer:

    [1, 2] -> Option D
  4. Quick Check:

    Deleting last node removes 3, leaving [1, 2] [OK]
Hint: Deleting end removes last node, so last value drops [OK]
Common Mistakes:
  • Forgetting to update the second last node's next to null
  • Assuming the head changes incorrectly
  • Not handling single-node list case
4. The following code tries to delete the last node of a singly linked list but causes an error:
deleteAtEnd() {
  let current = this.head;
  while (current.next) {
    current = current.next;
  }
  current = null;
}

What is the main problem?
medium
A. It sets the local variable to null but does not update the list
B. It does not handle empty list case
C. It deletes the head node instead of last node
D. It causes infinite loop

Solution

  1. Step 1: Analyze the loop and pointer update

    The loop moves current to the last node, but setting current = null only changes the local variable, not the list's links.
  2. Step 2: Understand why the last node is not removed

    To remove the last node, the previous node's next must be set to null. This code misses that step.
  3. Final Answer:

    It sets the local variable to null but does not update the list -> Option A
  4. Quick Check:

    Local variable null doesn't remove node [OK]
Hint: Update previous node's next, not local pointer [OK]
Common Mistakes:
  • Setting current to null instead of previous.next
  • Not checking for single-node list
  • Ignoring empty list case
5. You have a singly linked list with nodes: 10 -> 20 -> 30 -> 40. You want to delete the last node and then add a new node with value 50 at the end. Which sequence of operations correctly achieves this?
hard
A. Call deleteAtEnd(), then call addAtEnd(50)
B. Call addAtEnd(50), then call deleteAtEnd()
C. Call deleteAtEnd() twice, then addAtEnd(50)
D. Call deleteAtEnd(), then addAtStart(50)

Solution

  1. Step 1: Delete the last node

    Calling deleteAtEnd() removes the node with value 40, list becomes 10 -> 20 -> 30.
  2. Step 2: Add new node at the end

    Calling addAtEnd(50) adds a new node with value 50 at the end, list becomes 10 -> 20 -> 30 -> 50.
  3. Final Answer:

    Call deleteAtEnd(), then call addAtEnd(50) -> Option A
  4. Quick Check:

    Delete last then add new last node [OK]
Hint: Delete last first, then add new node at end [OK]
Common Mistakes:
  • Adding before deleting causes wrong order
  • Deleting twice removes too many nodes
  • Adding at start changes list order
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