Insert at the Head of a Linked List
You are given a singly linked list (as an array of node values from head → tail) and an integer value. Create a new node carrying that value and place it at…
You are given a singly linked list (as an array of node values from **head → tail**) and an integer value. Create a new node carrying that value and place it at the very front of the list so that it becomes the new first node, then return the head of the updated list. The existing chain must stay intact behind the new node, and the operation must run in constant time regardless of list length. If the list starts out empty, the new node becomes the only node and is itself the head. For example, with `head = [4, 7, 2, 9]` and `value = 5`, the result is `[5, 4, 7, 2, 9]`.
- `0 <= nodes.length <= 10^5` - `-10^9 <= nodes[i] <= 10^9` - `-10^9 <= value <= 10^9` - The insertion must run in constant time regardless of list length.
Example 1: input head = [4,7,2,9], value = 5; output [5, 4, 7, 2, 9]. A new node holding 5 is created, its next pointer is set to the old head (4), then the head moves to the new node.
Example 2: input head = [], value = 3; output [3]. The list is empty, so the new node's next is null. It is the only node and the head.
Example 3: input head = [8], value = 1; output [1, 8]. The new node holding 1 points to the existing node 8 and becomes the head — 1 → 8.
Category: Linked List. Topic: linked-list. Difficulty: Easy. Patterns: linked-list-traversal.