You are given the head of a singly linked list and an integer x. Insert x at the very beginning of the list and return the head of the modified list.
The new value always becomes the first node, sitting in front of every existing node. The rest of the list keeps its exact order and values — only the front of the list changes.
Because the front of a list is directly reachable through head, this can be done in constant time without walking the list.
Input: head = [2, 10], x = 1 Output: 1 -> 2 -> 10 The value 1 is placed at the front, so it becomes the new head; 2 -> 10 follow unchanged.
Input: head = [2, 3, 4, 5], x = 1 Output: 1 -> 2 -> 3 -> 4 -> 5 1 is inserted at the beginning, ahead of the existing 2 -> 3 -> 4 -> 5.
- 1 <= number of nodes <= 10^5 - 1 <= node value <= 10^3 - 1 <= x <= 10^3
Inserting at the head of a linked list is the twin of deleting it — and it teaches the same core truth: a linked list is only ever whatever the head pointer points to. Create one node, aim it at the current front, and move head — the whole list just grew by one, in constant time.
next pointer to the following node; the last node's next is null.head — whatever it points to is the first node, and everything reachable from there is the list.next pointer is all it takes to grow the list.In plain terms: build a node holding x, point its next at the current head, and declare that new node the head.
Worked example — head = [2, 10], x = 1
before: head → [2] → [10] → null x = 1 create: new node [1], set [1].next = head (points at [2]) move: head = [1] after: head → [1] → [2] → [10] → null answer: 1 -> 2 -> 10
Confirming the input's shape before writing a pointer move is what separates a careful engineer from a lucky one.
“Can the incoming list be empty?”
If it can, the new node's next is simply null and it becomes a one-element list — worth confirming the return type handles it.
“Does x always go first, even if it duplicates an existing value?”
Yes — position, not value, decides placement, so a duplicate value still goes at the very front.
“How large can the list get?”
Up to 10^5 nodes, so an O(n) rebuild is wasteful — the intended move is O(1).
Before I code, let me confirm a couple of things.
I insert x strictly at the front, so it becomes the new head regardless of its value — correct?
And if the list were empty, x would become a single-node list, which my return handles.
Before you touch head, capture what it references. The new node's next has to be the current front so the rest of the list stays attached.
new.next = head # new → [old front] → ... head = new # head now starts at the new node
Set the new node's next to the old head first, then move head. If you reassign head before saving it, you lose the reference to the rest of the list.
Every existing node keeps its value and its next. You add exactly one node and rewire one pointer, so the work is O(1) no matter how long the list is.
| Rebuild the list | Prepend a node | |
|---|---|---|
| Idea | Copy x then every existing node into a fresh list | Make one node, point it at head, move head |
| Time | O(n) | O(1) |
| Space | O(n) | O(1) |
Rebuilding recreates data that already exists in memory. The optimal move allocates a single node and rewires one pointer — see the full code in the Approaches selector below.
Key takeaway
To insert at the front of a singly linked list, create a node for x, set its next to the current head, and make it the new head. Because the list is defined by its head pointer, this is a constant-time operation — no traversal, no copying.
node = new Node(x) node.next = head head = node return head