Insert at the Tail of a Linked List

You are given a singly linked list (as an array of node values from head → tail) and an integer value. Append a brand-new node carrying that value to the very…

You are given a singly linked list (as an array of node values from **head → tail**) and an integer value. Append a brand-new node carrying that value to the very end of the chain, then return the head of the updated list. There is no maintained tail pointer, so reaching the last node requires walking the chain from the head until you arrive at the node whose next link is null. If the list is empty (the head is null), the new node itself becomes the head of a one-node list. For example, with `head = [4, 7, 2]` and `value = 9`, the result is `[4, 7, 2, 9]`.

- `0 <= nodes.length <= 10^5` - `-10^9 <= nodes[i] <= 10^9` - `-10^9 <= value <= 10^9` - The new node is always added after the current last node, never in the middle.

Example 1: input head = [4,7,2], value = 9; output [4, 7, 2, 9]. Walk from node 4 to node 7 to node 2 (the last node, whose next is null). Link node 2's next to a new node holding 9.

Example 2: input head = [], value = 5; output [5]. The list is empty, so there is nothing to traverse. The new node holding 5 becomes the head.

Example 3: input head = [8], value = 3; output [8, 3]. The single node 8 is already the last node. Link its next to a new node holding 3, which ends the list.

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