Delete the Tail of a Linked List

You are given a singly linked list (as an array of node values from head → tail). Remove the last node of the list and return the head of the list that…

You are given a singly linked list (as an array of node values from **head → tail**). Remove the last node of the list and return the head of the list that remains. Because a singly linked list has no backward pointers, you must stop your walk on the second-to-last node and set its next link to null so the final node drops off. If the list is empty, there is nothing to remove, so return an empty list (null head). If the list has exactly one node, removing it leaves an empty list, so return an empty list as well. For example, with `head = [4, 9, 2, 7]`, the result is `[4, 9, 2]`.

- `0 <= nodes.length <= 10^5` - `-10^9 <= nodes[i] <= 10^9` - The input is one finite, proper acyclic singly linked list. - The taught optimal approach modifies the existing tail link without allocating a new list.

Example 1: input head = [4,9,2,7]; output [4, 9, 2]. The last node holds 7. Walk to the node holding 2 (the second-to-last), set its next to null, and the node holding 7 drops off the chain.

Example 2: input head = [15]; output []. A single node is itself the tail. Removing it leaves an empty list whose head is null.

Example 3: input head = []; output []. There is no node to remove, so the empty list is returned unchanged.

Category: Linked List. Topic: linked-list. Difficulty: Easy. Patterns: linked-list-traversal.