Linked List February 21 ,2026

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

  1. If head is NULL → return NULL
  2. Store current head in a temporary variable
  3. Move head to head.next
  4. Delete/free the old head
  5. 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

  1. Create a dummy node
  2. Set dummy.next = head
  3. If head exists
    • Delete dummy.next (original head)
    • Move dummy.next to next node
  4. 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

 

Next Problem in the series-

Delete Last Node of Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com