Deletion at the end of a Linked List

easy

Given the head of a singly linked list, delete the tail — the last node — and return the head of the modified list.

The tail is the only node whose next pointer is null. Removing it means the node just before it becomes the new last node, so its next must be set to null.

If the list has a single node, deleting it leaves an empty list, so you return a null head.

Hints

To remove the last node, ask yourself: which node's next pointer actually has to change?
You cannot touch the tail directly — you need the node right before it, and a singly linked list has no backward pointer.
Walk one pointer forward until curr.next.next is null; curr is now the second-last node, so set curr.next to null.

Common doubts

The head is directly available, but the tail's predecessor is not — a singly linked list has no backward pointer, so you must walk from the front to reach it, which is O(n).
Deleting that single node leaves an empty list, so you return null (an empty head).
You want to land on the node just before the tail. If you walk until curr.next is null, you have gone one step too far and are standing on the tail itself, with nothing left to cut.

Interview follow-ups

Use two pointers k apart, then advance both until the front one hits the end — the trailing pointer lands on the node before the target, ready to splice it out in one pass.
With a backward pointer (or a maintained tail pointer) you reach the last node's predecessor in O(1), so deleting the tail becomes constant time instead of O(n).

Fun facts

  • Deleting the tail of a singly linked list is O(n), but the same operation on a doubly linked list (or one that caches a tail pointer) is O(1) — the extra backward link pays for itself.
  • This 'walk to the second-last node' pattern is the seed of the two-pointer trick used to remove the n-th node from the end in a single pass.

Asked at

AmazonMicrosoftAdobe
Frequently Sometimes Occasionally
Example 1
Input: head = [1, 2, 3, 4, 5]
Output: 1 -> 2 -> 3 -> 4
The last node (value 5) is removed, leaving 1 -> 2 -> 3 -> 4.
Example 2
Input: head = [3, 12, 9]
Output: 3 -> 12
Dropping the tail node (value 9) leaves 3 -> 12.
Constraints

- 1 <= number of nodes <= 10^5 - 1 <= node value <= 10^5

Solve this problem →