Deletion at beginning (Removal of first node) in a Linked List
Last Updated : 21 Oct, 2025
Given a head of the linked list, we need to remove the first node from the given linked list.
Examples:
Input :
Output : 2 -> 3 -> 1 -> 7 -> NULL Explanation: After deleting head from the given linked list, we'll be left with just 2 -> 3 -> 1 -> 7.
Input :
Output : 5 -> 7 -> 8 -> 99 -> 100 -> NULL Explanation: After deleting head from the given linked list, we'll be left with just 5 -> 7 -> 8 -> 99 -> 100.
By Shifting head node to next node of head - O(1) Time and O(1) Space
To remove the first node of a linked list, store the current head in a temporary variable (temp), move the head pointer to the next node, delete the temporary head node and finally , return the new head of the linked list.
Below is the implementation of the above approach:
C++
#include<iostream>usingnamespacestd;classNode{public:intdata;Node*next;Node(intx){data=x;next=nullptr;}};// Delete the head node and return the new headNode*deleteHead(Node*head){// Check if the list is emptyif(head==nullptr)returnnullptr;// Store the current head in a// temporary variableNode*temp=head;// Move the head pointer to the next nodehead=head->next;// Free the memory of the old head nodedeletetemp;returnhead;}// Function to print the linked listvoidprintList(Node*curr){while(curr!=nullptr){cout<<curr->data;if(curr->next!=nullptr){cout<<" -> ";}curr=curr->next;}}intmain(){// Create a hard-coded linked list:// 8 -> 2 -> 3 -> 1 -> 7Node*head=newNode(8);head->next=newNode(2);head->next->next=newNode(3);head->next->next->next=newNode(1);head->next->next->next->next=newNode(7);head=deleteHead(head);printList(head);return0;}
C
#include<stdio.h>#include<stdlib.h>// Define a linked list nodestructNode{intdata;structNode*next;};// Function to create a new nodestructNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->next=NULL;returnnewNode;}// Delete the head node and return the new headstructNode*deleteHead(structNode*head){if(head==NULL)returnNULL;structNode*temp=head;head=head->next;free(temp);// Free memory of old headreturnhead;}// Function to print the linked listvoidprintList(structNode*curr){while(curr!=NULL){printf("%d",curr->data);if(curr->next!=NULL)printf(" -> ");curr=curr->next;}printf("\n");}intmain(){// Create a hard-coded linked list: 8 -> 2 -> 3 -> 1 -> 7structNode*head=createNode(8);head->next=createNode(2);head->next->next=createNode(3);head->next->next->next=createNode(1);head->next->next->next->next=createNode(7);// Delete the head nodehead=deleteHead(head);// Print the updated listprintList(head);return0;}
Java
classNode{intdata;Nodenext;Node(intx){data=x;next=null;}}classGfG{// Delete the head node and return the new headstaticNodedeleteHead(Nodehead){// Check if the list is emptyif(head==null)returnnull;// Store the current head in a// temporary variableNodetemp=head;// Move the head pointer to the next nodehead=head.next;// Free the memory of the old head nodetemp=null;returnhead;}// Function to print the linked liststaticvoidprintList(Nodecurr){while(curr!=null){System.out.print(curr.data);if(curr.next!=null){System.out.print(" -> ");}curr=curr.next;}}publicstaticvoidmain(String[]args){// Create a hard-coded linked list:// 8 -> 2 -> 3 -> 1 -> 7Nodehead=newNode(8);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(1);head.next.next.next.next=newNode(7);head=deleteHead(head);printList(head);}}
Python
classNode:def__init__(self,x):self.data=xself.next=None# Delete the head node and return the new headdefdeleteHead(head):# Check if the list is emptyifheadisNone:returnNone# Store the current head in a# temporary variabletemp=head# Move the head pointer to the next nodehead=head.next# Free the memory of the old head node# (Python garbage collector will handle it)temp=Nonereturnhead# Function to print the linked listdefprintList(curr):whilecurrisnotNone:print(curr.data,end="")ifcurr.nextisnotNone:print(" -> ",end="")curr=curr.nextif__name__=="__main__":# Create a hard-coded linked list:# 8 -> 2 -> 3 -> 1 -> 7head=Node(8)head.next=Node(2)head.next.next=Node(3)head.next.next.next=Node(1)head.next.next.next.next=Node(7)head=deleteHead(head)printList(head)
C#
usingSystem;classNode{publicintdata;publicNodenext;publicNode(intx){data=x;next=null;}}classGfG{// Delete the head node and return the new headpublicstaticNodeDeleteHead(Nodehead){// Check if the list is emptyif(head==null)returnnull;// Move the head pointer to the next nodeNodetemp=head;head=head.next;// Free the memory of the old head nodetemp=null;returnhead;}// Function to print the linked listpublicstaticvoidPrintList(Nodecurr){while(curr!=null){Console.Write(curr.data);if(curr.next!=null){Console.Write(" -> ");}curr=curr.next;}Console.WriteLine();}publicstaticvoidMain(string[]args){// Create a hard-coded linked list:// 8 -> 2 -> 3 -> 1 -> 7Nodehead=newNode(8);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(1);head.next.next.next.next=newNode(7);head=DeleteHead(head);PrintList(head);}}
JavaScript
classNode{constructor(x){this.data=x;this.next=null;}}classGfG{// Delete the head node and return the new headstaticdeleteHead(head){// Check if the list is emptyif(head===null)returnnull;// Move the head pointer to the next nodelettemp=head;head=head.next;// Free the memory of the old head node (not needed in JS, GC handles it)temp=null;returnhead;}// Function to print the linked liststaticprintList(curr){while(curr!==null){process.stdout.write(curr.data.toString());if(curr.next!==null){process.stdout.write(" -> ");}curr=curr.next;}console.log();}}// Driver code// Create a hard-coded linked list:// 8 -> 2 -> 3 -> 1 -> 7lethead=newNode(8);head.next=newNode(2);head.next.next=newNode(3);head.next.next.next=newNode(1);head.next.next.next.next=newNode(7);head=GfG.deleteHead(head);GfG.printList(head);
Output
2 -> 3 -> 1 -> 7
Time Complexity: O(1), because the operation to delete the head node is performed in constant time. Space Complexity: O(1)