Delete the First Node of a Linked List

Solve this Problem
Easy5–10 min
Topics
Companies
Practice:GFG ↗
Given the head of a singly linked list, delete the first node and return the head of the resulting list. If the list is already empty, return it unchanged. Since the second node is already fully linked to the rest of the list, deleting the first node is really just a matter of deciding what counts as the head now. The optimal solution never touches, copies, or frees anything — it simply reassigns head to head.next, in O(1).

Test Case 1:

Input:head = [8, 2, 3, 1, 7]
Output:[2, 3, 1, 7]
Explanation:The first node (8) is removed — the second node becomes the new head.

Test Case 2:

Input:head = [5]
Output:[]
Explanation:Deleting the only node leaves an empty list.

Test Case 3:

Input:head = []
Output:[]
Explanation:Nothing to delete — the empty list stays empty.

Constraints

  • 0 ≤ number of nodes in head ≤ 10⁴
  • -10⁹ ≤ node value ≤ 10⁹
  • Deleting from an empty list must return an empty list, not an error
🚀

Try the Dry Run

Don't just read the solution — watch it execute, one step at a time.

🧪Try your own test case
1class Solution {
2 public Node deleteAtBeginning(Node head) {
3 if (head == null) {
4 return null;
5 }
6 return head.next;
7 }
8}
9
4
9
1
null
Variables
head4
COMPARE

head is not null, so skip the empty-list base case and drop straight to reassigning head.

Step 1 / 2

Approach & Solutions

Brute Force — Copy to Array Except the First Node, Rebuild List

Brute

Walk the list starting from the second node, copying every value except the first into a plain array. Throw the entire original list away and build a brand-new list from that array using a dummy + tail pointer. Correct, but it reads and re-allocates n - 1 nodes just to drop one.

TimeO(n)
SpaceO(n)
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node deleteAtBeginning(Node head) { 9 List<Integer> vals = new ArrayList<>(); 10 Node curr = (head == null) ? null : head.next; 11 while (curr != null) { 12 vals.add(curr.val); 13 curr = curr.next; 14 } 15 Node dummy = new Node(0); 16 Node tail = dummy; 17 for (int v : vals) { 18 tail.next = new Node(v); 19 tail = tail.next; 20 } 21 return dummy.next; 22 } 23}

Optimal — Move the Head Pointer Forward

Optimal

The second node is already fully formed and correctly linked to the rest of the list — it just needs to become the new head. Reassigning head to head.next does exactly that; the old first node is never touched, just left unreachable.

TimeO(1)
SpaceO(1)
1// Node definition used in this problem: 2// class Node { 3// int val; 4// Node next; 5// } 6 7class Solution { 8 public Node deleteAtBeginning(Node head) { 9 if (head == null) { 10 return null; 11 } 12 return head.next; 13 } 14}

Related Problems