Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Deletion at end (Removal of last node) in a Linked List
Next article icon

Deletion at beginning (Removal of first node) in a Linked List

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a linked list, The task is to remove the first node from the given linked list.

Examples:

Input : head : 3 -> 12 -> 15 -> 18 -> NULL
Output : 12 -> 15 -> 18 -> NULL

Input : head : 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output : 4 -> 6 -> 8 -> 33 -> 67 -> NULL

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.

Deletion-at-beginning-

Deletion at beginning in a Linked List

Below is the implementation of the above approach:

C++
// C++ program to delete the head node  // from a linked list  #include <iostream> using namespace std;  struct Node {     int data;     Node *next;     Node(int x) {         data = x;         next = nullptr;     } };  // Delete the head node and return the new head Node* deleteHead(Node* head) {        // Check if the list is empty     if (head == nullptr)         return nullptr;      // Store the current head in a   	// temporary variable     Node* temp = head;      // Move the head pointer to the next node     head = head->next;      // Free the memory of the old head node     delete temp;      return head; }  void printList(Node* curr) {     while (curr != nullptr) {       	cout << curr->data << " ";         curr = curr->next;      } }  int main() {        // Create a hard-coded linked list:     // 3 -> 12 -> 15 -> 18     Node* head = new Node(3);     head->next = new Node(12);     head->next->next = new Node(15);     head->next->next->next = new Node(18);     head = deleteHead(head);     printList(head);      return 0; } 
C
// C program to delete the head node // from a linked list  #include <stdio.h> #include <stdlib.h>  struct Node {     int data;     struct Node* next; };  // Delete the head node and return the new head struct Node* deleteHead(struct Node* head) {        // Check if the list is empty     if (head == NULL)         return NULL;      // Store the current head in a    	// temporary variable     struct Node* temp = head;      // Move the head pointer to the next node     head = head->next;      // Free the memory of the old head node     free(temp);      return head; }  void printList(struct Node* curr) {   	     while (curr != NULL) {         printf("%d ", curr->data);         curr = curr->next;     } } struct Node *createNode(int new_data){     struct Node *new_node =       (struct Node *)malloc(sizeof(struct Node));     new_node->data = new_data;     new_node->next = NULL;     return new_node; }  int main() {        // Create a hard-coded linked list:     // 3 -> 12 -> 15 -> 18     struct Node* head = createNode(3);     head->next = createNode(12);     head->next->next = createNode(15);     head->next->next->next = createNode(18);     head = deleteHead(head);     printList(head);      return 0; } 
Java
// Java program to delete the head node // from a linked list  class Node {     int data;     Node next;      Node(int data) {         this.data = data;         this.next = null;     } }  public class GfG {      // Delete the head node and return the new head     static Node deleteHead(Node head) {                // Check if the list is empty         if (head == null)             return null;          // Store the current head in a temporary variable         Node temp = head;          // Move the head pointer to the next node         head = head.next;          // Help the garbage collector by        	// removing the reference         temp = null;          return head;     }      static void printList(Node curr) {         while (curr != null) {             System.out.print(curr.data + " ");             curr = curr.next;         }     }      public static void main(String[] args) {                 // Create a hard-coded linked list:         // 3 -> 12 -> 15 -> 18         Node head = new Node(3);         head.next = new Node(12);         head.next.next = new Node(15);         head.next.next.next = new Node(18);         head = deleteHead(head);         printList(head);     } } 
Python
# Python program to delete the head node  # from a linked list  class Node:     def __init__(self, data):         self.data = data         self.next = None  # Delete the head node and return the new head def delete_head(head):        # Check if the list is empty     if head is None:         return None      # Store the current head in a temporary variable     temp = head      # Move the head pointer to the next node     head = head.next      # Delete the old head by removing the reference     del temp      return head  def print_list(curr):     while curr:         print(curr.data, end=" ")         curr = curr.next  if __name__ == "__main__":         # Create a hard-coded linked list:     # 3 -> 12 -> 15 -> 18     head = Node(3)     head.next = Node(12)     head.next.next = Node(15)     head.next.next.next = Node(18)     head = delete_head(head)     print_list(head) 
C#
// C# program to delete the head node // from a linked list  using System;  class Node {     public int data;     public Node next;      public Node(int data) {         this.data = data;         this.next = null;     } }  class GfG {      // Delete the head node and return the new head     static Node DeleteHead(Node head) {                // Check if the list is empty         if (head == null)             return null;          // Move the head pointer to the next node         head = head.next;          return head;     }      static void PrintList(Node curr) {         while (curr != null) {             Console.Write(curr.data + " ");             curr = curr.next;         }     }      static void Main() {            // Create a hard-coded linked list:         // 3 -> 12 -> 15 -> 18         Node head = new Node(3);         head.next = new Node(12);         head.next.next = new Node(15);         head.next.next.next = new Node(18);         head = DeleteHead(head);         PrintList(head);     } } 
JavaScript
// JavaScript program to delete the head //node from a linked list  class Node {     constructor(data) {         this.data = data;         this.next = null;     } }  // Delete the head node and return the new head function deleteHead(head) {      // Check if the list is empty     if (head === null)         return null;      // Store the current head in a temporary variable     let temp = head;      // Move the head pointer to the next node     head = head.next;      // Free the memory of the old head node     temp = null;      return head; }  function printList(curr) {     while (curr !== null) {         console.log(curr.data + " ");         curr = curr.next;     }  }  // Create a hard-coded linked list: // 3 -> 12 -> 15 -> 18 let head = new Node(3); head.next = new Node(12); head.next.next = new Node(15); head.next.next.next = new Node(18);  head = deleteHead(head); printList(head); 

Output
12 15 18 

Time Complexity: O(1), because the operation to delete the head node is performed in constant time.
Space Complexity: O(1)



Next Article
Deletion at end (Removal of last node) in a Linked List

G

gfg_sal_gfg
Improve
Article Tags :
  • DSA
  • Linked List
  • Technical Scripter
  • Technical Scripter 2018
Practice Tags :
  • Linked List

Similar Reads

  • Deletion at beginning (Removal of first node) in a Doubly Linked List
    Given a doubly linked list, the task is to delete the node from the beginning of the linked list. Examples: Input : 1 <-> 2 <-> 3 -> NULLOutput : 2 <-> 3 <-> NULL Input : 2 <-> 4 <-> 6 <-> 8 <-> 33 <-> 67 <-> NULLOutput : 4 <-> 6
    7 min read
  • Deletion at end (Removal of last node) in a Linked List
    Given a linked list, the task is to delete the last node of the given linked list. Examples:   Input: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput: 1 -> 2 -> 3 -> 4 -> NULL Explanation: The last node of the linked list is 5, so 5 is deleted. Input: 3 -> 12 -> 15-> NULLOutp
    8 min read
  • Delete Kth nodes from the beginning and end of a Linked List
    Given a singly Linked List and an integer K denoting the position of a Linked List, the task is to delete the Kth node from the beginning and end of the Linked List. Examples: Input: 1 ? 2 ? 3 ? 4 ? 5 ? 6, K = 3Output: 1 ? 2 ? 5 ? 6Explanation: Deleted Nodes : 3, 4 Input: 1 ? 2 ? 3 ? 4 ? 5 ? 6, K =
    13 min read
  • Deletion at end (Removal of last node) in a Doubly Linked List
    Given a doubly linked list, the task is to delete the last node of the given linked list. Examples: Input: 1 <-> 2 <-> 3 <-> NULLOutput: 1 <-> 2 <-> NULLExplanation: The last node of the linked list is 3, so 3 is deleted. Input: 15 -> NULLOutput: NULLExplanation: The
    7 min read
  • Insert a Node at Front/Beginning of a Linked List
    Given a linked list, the task is to insert a new node at the beginning/start/front of the linked list. Example: Input: LinkedList = 2->3->4->5, NewNode = 1Output: 1 2 3 4 5 Input: LinkedList = 2->10, NewNode = 1Output: 1 2 10 Approach: To insert a new node at the front, we create a new n
    8 min read
  • Swap first odd and even valued nodes from the beginning and end of a Linked List
    Given a singly Linked List, the task is to swap the first odd valued node from the beginning and the first even valued node from the end of the Linked List. If the list contains node values of a single parity, then no modifications are required. Examples: Input: 4 -> 3 -> 5 -> 2 -> 3 -
    13 min read
  • Insert Node at the End of a Linked List
    Given a linked list, the task is to insert a new node at the end of the linked list. Examples: Input: LinkedList = 2 -> 3 -> 4 -> 5, NewNode = 1Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1 Input: LinkedList = NULL, NewNode = 1Output: LinkedList = 1 Approach:  Inserting at the end
    9 min read
  • Find first node of loop in a linked list
    Given the head of a linked list that may contain a loop. A loop means that the last node of the linked list is connected back to a node in the same list. The task is to find the Starting node of the loop in the linked list if there is no loop in the linked list return -1. Example: Input: Output: 3Ex
    14 min read
  • Move first element to end of a given Linked List
    Write a C function that moves first element to end in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 2->3->4->5->1. Algorithm: Traverse the list till last node. Use two pointers: one to store the
    14 min read
  • Insert a node in Linked List before a given node
    Given a linked list, the task is to insert a new node with a specified value into a linked list before a node with a given key. Examples Input: head: 1 -> 2 -> 3 -> 4 -> 5 , newData = 6, key = 2Output: 1 -> 6 -> 2 -> 3 -> 4 -> 5 Explanation: After inserting node with value
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences