Delete First Node of Linked List
Problem
Given the head of a Linked List, delete the first node (head node) and return the updated head of the list.
After deletion:
- The second node becomes the new head
- Memory of the old head should be freed (in languages like C/C++)
- If the list is empty, return NULL
- If the list has only one node, the list becomes empty
Example
Input
10 → 20 → 30 → 40
Output after deleting first node
20 → 30 → 40
Approach 1 — Move Head Pointer
Idea
Simply move the head pointer to the next node.
Since the first node has no previous dependencies, deletion is straightforward.
Steps
- If head is NULL → return NULL
- Store current head in a temporary variable
- Move head to head.next
- Delete/free the old head
- Return updated head
Time Complexity
O(1)
Space Complexity
O(1)
C Implementation
// While running this code, replace all #include "..." with #include <...>
#include "stdio.h"
#include "stdlib.h"
struct Node {
int data;
struct Node* next;
};
struct Node* deleteFirst(struct Node* head) {
if (head == NULL)
return NULL;
struct Node* temp = head;
head = head->next;
free(temp);
return head;
}
void printList(struct Node* head) {
while (head) {
printf("%d ", head->data);
head = head->next;
}
}
int main() {
// Creating list: 10 -> 20 -> 30
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = (struct Node*)malloc(sizeof(struct Node));
head->next->data = 20;
head->next->next = (struct Node*)malloc(sizeof(struct Node));
head->next->next->data = 30;
head->next->next->next = NULL;
head = deleteFirst(head);
printList(head);
return 0;
}
Output
20 30
Java Implementation
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class Main {
static Node deleteFirst(Node head) {
if (head == null)
return null;
return head.next;
}
static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head = deleteFirst(head);
printList(head);
}
}
Output
20 30
Python Implementation
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_first(head):
if not head:
return None
return head.next
def print_list(head):
while head:
print(head.data, end=" ")
head = head.next
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head = delete_first(head)
print_list(head)
Output
20 30
JavaScript Implementation
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteFirst(head) {
if (head === null)
return null;
return head.next;
}
function printList(head) {
while (head !== null) {
process.stdout.write(head.data + " ");
head = head.next;
}
}
let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head = deleteFirst(head);
printList(head);
Output
20 30
Approach 2 — Using Dummy Node
Idea
In this approach, we introduce a dummy node (also called a sentinel node) that points to the head of the linked list.
This technique is widely used in complex linked list problems because:
- It simplifies edge case handling
- Avoids special handling when deleting the head
- Keeps operations uniform
After deletion, we return dummy.next as the new head.
Steps
- Create a dummy node
- Set dummy.next = head
- If head exists
- Delete dummy.next (original head)
- Move dummy.next to next node
- Return dummy.next as new head
Time Complexity
O(1)
Space Complexity
O(1)
C Implementation
// While running this code, replace all #include "..." with #include <...>
#include "stdio.h"
#include "stdlib.h"
struct Node {
int data;
struct Node* next;
};
struct Node* deleteFirst(struct Node* head) {
struct Node dummy;
dummy.next = head;
if (dummy.next != NULL) {
struct Node* temp = dummy.next;
dummy.next = temp->next;
free(temp);
}
return dummy.next;
}
void printList(struct Node* head) {
while (head) {
printf("%d ", head->data);
head = head->next;
}
}
int main() {
// Creating list: 10 -> 20 -> 30
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = 10;
head->next = (struct Node*)malloc(sizeof(struct Node));
head->next->data = 20;
head->next->next = (struct Node*)malloc(sizeof(struct Node));
head->next->next->data = 30;
head->next->next->next = NULL;
head = deleteFirst(head);
printList(head);
return 0;
}Output
20 30
C++ Implementation
// While running this code, replace all #include "..." with #include <...>
#include "iostream"
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
Node* deleteFirst(Node* head) {
Node dummy(0);
dummy.next = head;
if (dummy.next != NULL) {
Node* temp = dummy.next;
dummy.next = temp->next;
delete temp;
}
return dummy.next;
}
void printList(Node* head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
}
int main() {
// Creating list: 10 -> 20 -> 30
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head = deleteFirst(head);
printList(head);
return 0;
}
Output
20 30
Java Implementation
class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
public class Main {
static Node deleteFirst(Node head) {
Node dummy = new Node(0);
dummy.next = head;
if (dummy.next != null) {
dummy.next = dummy.next.next;
}
return dummy.next;
}
static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head = deleteFirst(head);
printList(head);
}
}
Output
20 30
Python Implementation
class Node:
def __init__(self, data):
self.data = data
self.next = None
def delete_first(head):
dummy = Node(0)
dummy.next = head
if dummy.next:
dummy.next = dummy.next.next
return dummy.next
def print_list(head):
while head:
print(head.data, end=" ")
head = head.next
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head = delete_first(head)
print_list(head)
Output
20 30
JavaScript Implementation
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteFirst(head) {
let dummy = new Node(0);
dummy.next = head;
if (dummy.next !== null) {
dummy.next = dummy.next.next;
}
return dummy.next;
}
function printList(head) {
while (head !== null) {
process.stdout.write(head.data + " ");
head = head.next;
}
}
let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head = deleteFirst(head);
printList(head);
Output
20 30
