Delete the Tail of a Doubly Linked List

You are given a doubly linked list (as an array of node values from head to tail), where every node carries a value, a next link, and a prev (backward) link.…

You are given a doubly linked list (as an array of node values from head to tail), where every node carries a value, a next link, and a prev (backward) link. Remove the last node of the list and return the head of the list that remains. Because each node remembers its predecessor through a prev link, you can walk all the way to the final node and then step back once to reach the second-to-last node, setting its next link to null so the tail 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 = [5, 1, 6, 3]`, the result is `[5, 1, 6]`.

- `0 <= nodes.length <= 10^5` - `-10^9 <= nodes[i] <= 10^9` - Modify the existing next and prev links in place; do not allocate a new list.

Example 1: input head = [5,1,6,3]; output [5, 1, 6]. The last node holds 3. Walk to it, follow its prev link back to the node holding 6 (the second-to-last), set that node's next to null, and the node holding 3 drops off the chain.

Example 2: input head = [12]; 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.