Insert a Node at a Given Position in a Linked List
Solve this Problemhead of a singly linked list, an integer val, and a 0-indexed position, insert a new node holding val so that it ends up at index position in the resulting list, and return the head.
position = 0 is exactly the insert-at-the-beginning case, and position equal to the list's current length is exactly the insert-at-the-end case — this problem generalizes both. The optimal solution walks to the node just before the target index and redirects two pointers, without touching any other existing node.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
0 ≤ number of nodes in head ≤ 10⁴ - ◆
-10⁹ ≤ node value, val ≤ 10⁹ - ◆
0 ≤ position ≤ length of head — position is 0-indexed, so position 0 means the new head and position length means the new tail
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public Node insertAtPosition(Node head, int val, int position) { |
| 3 | Node newNode = new Node(val); |
| 4 | if (position == 0) { |
| 5 | newNode.next = head; |
| 6 | return newNode; |
| 7 | } |
| 8 | Node curr = head; |
| 9 | for (int i = 0; i < position - 1; i++) { |
| 10 | curr = curr.next; |
| 11 | } |
| 12 | newNode.next = curr.next; |
| 13 | curr.next = newNode; |
| 14 | return head; |
| 15 | } |
| 16 | } |
| 17 |
3Create a new node holding 3. It isn't linked into the list yet.
Approach & Solutions
Brute Force — Copy to Array, Splice, Rebuild List
BruteWalk the existing list into a plain array, then splice val into that array at the given position, shifting every later element over by one. Throw the entire original list away and build a brand-new list from the array using a dummy + tail pointer. Correct, but every existing node is read and re-allocated even though only one new node actually needed to be spliced in.
O(n)O(n)1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node insertAtPosition(Node head, int val, int position) {
9 List<Integer> vals = new ArrayList<>();
10 Node curr = head;
11 while (curr != null) {
12 vals.add(curr.val);
13 curr = curr.next;
14 }
15 vals.add(position, val);
16 Node dummy = new Node(0);
17 Node tail = dummy;
18 for (int v : vals) {
19 tail.next = new Node(v);
20 tail = tail.next;
21 }
22 return dummy.next;
23 }
24}Optimal — Traverse to Position-1, Splice In
OptimalPosition 0 is really just insert-at-the-beginning — handle it the same O(1) way. Otherwise, walk from head to the node just before the target position, then redirect two pointers: the new node's next takes over what curr used to point at, and curr's next is redirected through the new node. No existing node is copied or re-allocated.
O(n)O(1) extra1// Node definition used in this problem:
2// class Node {
3// int val;
4// Node next;
5// }
6
7class Solution {
8 public Node insertAtPosition(Node head, int val, int position) {
9 Node newNode = new Node(val);
10 if (position == 0) {
11 newNode.next = head;
12 return newNode;
13 }
14 Node curr = head;
15 for (int i = 0; i < position - 1; i++) {
16 curr = curr.next;
17 }
18 newNode.next = curr.next;
19 curr.next = newNode;
20 return head;
21 }
22}