Remove first node of the linked list

24 Jun, 2022
Given a linked list, the task is to remove the first node of the linked list and update the head pointer of the linked list. Examples:  
Input : 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output : 2 -> 3 -> 4 -> 5 -> NULL

Input : 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output : 4 -> 6 -> 8 -> 33 -> 67 -> NULL
 
To remove first node, we need to make second node as head and delete memory allocated for first node.  
C++
Java
Python3
C#
Javascript
// CPP program to remove first node of// linked list.#include <iostream>using namespace std;  /* Link list node */struct Node {    int data;    struct Node* next;};  /* Function to remove the first node    of the linked list */Node* removeFirstNode(struct Node* head){    if (head == NULL)        return NULL;      // Move the head pointer to the next node    Node* temp = head;    head = head->next;      delete temp;      return head;}  // Function to push node at headvoid push(struct Node** head_ref, int new_data){    struct Node* new_node = new Node;    new_node->data = new_data;    new_node->next = (*head_ref);    (*head_ref) = new_node;}  // Driver codeint main(){    /* Start with the empty list */    Node* head = NULL;      /* Use push() function to construct         the below list 8 -> 23 -> 11 -> 29 -> 12 */    push(&head, 12);    push(&head, 29);    push(&head, 11);    push(&head, 23);    push(&head, 8);      head = removeFirstNode(head);    for (Node* temp = head; temp != NULL; temp = temp->next)        cout << temp->data << " ";      return 0;}
// Java program to remove first node of// linked list.class GFG {      // Link list node /    static class Node {        int data;        Node next;    };      // Function to remove the first node    // of the linked list /    static Node removeFirstNode(Node head)    {        if (head == null)            return null;          // Move the head pointer to the next node        Node temp = head;        head = head.next;          return head;    }      // Function to push node at head    static Node push(Node head_ref, int new_data)    {        Node new_node = new Node();        new_node.data = new_data;        new_node.next = (head_ref);        (head_ref) = new_node;        return head_ref;    }      // Driver code    public static void main(String args[])    {        // Start with the empty list /        Node head = null;          // Use push() function to con        // the below list 8 . 23 . 11 . 29 . 12 /        head = push(head, 12);        head = push(head, 29);        head = push(head, 11);        head = push(head, 23);        head = push(head, 8);          head = removeFirstNode(head);        for (Node temp = head; temp != null; temp = temp.next)            System.out.print(temp.data + " ");    }}  // This code is contributed by Arnab Kundu
# Python3 program to remove first node of  # linked list.import sys  # Link list node class Node:    def __init__(self, data):        self.data = data        self.next = None  # Function to remove the first node  # of the linked list def removeFirstNode(head):    if not head:        return None    temp = head      # Move the head pointer to the next node    head = head.next    temp = None    return head  # Function to push node at head def push(head, data):    if not head:        return Node(data)    temp = Node(data)    temp.next = head    head = temp    return head  # Driver codeif __name__=='__main__':      # Start with the empty list     head = None      # Use push() function to construct       # the below list 8 -> 23 -> 11 -> 29 -> 12     head = push(head, 12)    head = push(head, 29)    head = push(head, 11)    head = push(head, 23)    head = push(head, 8)      head = removeFirstNode(head)          while(head):        print("{} ".format(head.data), end ="")        head = head.next  # This code is Contributed by Vikash Kumar 37
// C# program to remove first node of// linked list.using System;   class GFG {      // Link list node /    public class Node     {        public int data;        public Node next;    };      // Function to remove the first node    // of the linked list /    static Node removeFirstNode(Node head)    {        if (head == null)            return null;          // Move the head pointer to the next node        Node temp = head;        head = head.next;          return head;    }      // Function to push node at head    static Node push(Node head_ref, int new_data)    {        Node new_node = new Node();        new_node.data = new_data;        new_node.next = (head_ref);        (head_ref) = new_node;        return head_ref;    }      // Driver code    public static void Main(String []args)    {        // Start with the empty list /        Node head = null;          // Use push() function to con        // the below list 8 . 23 . 11 . 29 . 12 /        head = push(head, 12);        head = push(head, 29);        head = push(head, 11);        head = push(head, 23);        head = push(head, 8);          head = removeFirstNode(head);        for (Node temp = head; temp != null; temp = temp.next)            Console.Write(temp.data + " ");    }}  /* This code contributed by PrinciRaj1992 */
<script>// javascript program to remove first node of// linked list.    // Link list node /class Node {    constructor(val) {        this.data = val;        this.next = null;    }}      // Function to remove the first node    // of the linked list /    function removeFirstNode( head) {        if (head == null)            return null;          // Move the head pointer to the next node         temp = head;        head = head.next;          return head;    }      // Function to push node at head    function push( head_ref , new_data) {         new_node = new Node();        new_node.data = new_data;        new_node.next = (head_ref);        (head_ref) = new_node;        return head_ref;    }      // Driver code              // Start with the empty list /         head = null;          // Use push() function to con        // the below list 8 . 23 . 11 . 29 . 12 /        head = push(head, 12);        head = push(head, 29);        head = push(head, 11);        head = push(head, 23);        head = push(head, 8);          head = removeFirstNode(head);        for ( temp = head; temp != null; temp = temp.next)            document.write(temp.data + " ");  // This code is contributed by todaysgaurav </script>
23 11 29 12
 
Time complexity : O(1) 
VishalBachchas
andrew1234
Vikash Kumar 37
princiraj1992
mohabmagdy79
todaysgaurav
Technical Scripter 2018
Linked List
Technical Scripter
Linked List
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Introduction to Data Structures
Doubly Linked List | Set 1 (Introduction and Insertion)
Merge two sorted linked lists
What is Data Structure: Types, Classifications and Applications
Linked List vs Array
Merge Sort for Linked Lists
Implementing a Linked List in Java using Class
Function to check if a singly linked list is palindrome
Queue - Linked List Implementation