Delete Head Node of Linked List

Problem Statement: For a given Singly LinkedList, Delete the First node of the linked list.

Examples:

Example 1:
Input: List = 10->20->30->40->null
Output: 20->30->40->null
Explanation: Deleted the head node.

Example 2:
Input: List = 100->200->300->null
Output: 200->300->null
Explanation: Deleted the head node.

Solution:

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Approach:

Deleting the First Node in LinkedList is 4 step process.

Step 1: First create a ListNode pointer temp, and make it point to the head of Linked List.

Step 2: We should delete the First node in LinkedList. So move the head to the next node after First Node. That means head should become head→next

Step 3: Temp is pointing to the previous head, temp→next is pointing to the newhead, as we are interested to delete the temp, Make the temp→next point null.
Step 4: We don’t require temp anymore, So delete the temp.

Visual representation of 4 step process for Deleting First Node of Single LinkedList

Code:

C++ Code

#include <bits/stdc++.h>

using namespace std;

class ListNode {
  public:
    int val;
  ListNode * next;
  ListNode(int x) {
    val = x;
    next = nullptr;
  }
};

ListNode * head, * tail; // head and tail of the LinkedList

void PrintList() // Function to print the LinkedList
{
  ListNode * curr = head;
  for (; curr != nullptr; curr = curr -> next)
    cout << curr -> val << "-->";
  cout << "null" << endl;
}

void InsertatLast(int value) // Function for creating a LinkedList
{

  ListNode * newnode = new ListNode(value);
  if (head == nullptr)
    head = newnode, tail = newnode;
  else
    tail = tail -> next = newnode;
}
void DeleteFirst() {
  if (head == nullptr)
    cout << "There are no nodes in LinkedList" << endl;
  else {
    // step1: Creating a Listnode pointer temp , and pointing it to head of LinkedList
    ListNode * temp = head;

    // Step2 : Making head->next as head
    head = head -> next;

    // Step3 : Making temp's next point to NULL
    temp -> next = nullptr;

    // step4 : Deleting the temp
    delete temp;
  }
}
int main() {
  InsertatLast(10);
  InsertatLast(20);
  InsertatLast(30);
  InsertatLast(40);
  cout << "LinkedList before Deleting First Node : " << endl;
  PrintList();
  DeleteFirst();
  cout << "LinkedList after Deleting First Node : " << endl;
  PrintList();
  return 0;
}

Output:

LinkedList before Deleting First Node :
10–>20–>30–>40–>null
LinkedList after Deleting First Node :
20–>30–>40–>null

Time Complexity:  O(1) Because we are just Manipulating the links, which is a constant operation.

Space Complexity: O(1) Constant Space.

Java Code

import java.util.*;

class ListNode {

  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
  }
}

class TUF {
  static ListNode head, tail; // head and tail of the LinkedList

  static void PrintList() // Function to print the LinkedList
  {
    ListNode curr = head;
    for (; curr != null; curr = curr.next)
      System.out.print(curr.val + "-->");
    System.out.println("null");
  }

  static void InsertatLast(int value) // Function for creating a LinkedList
  {

    ListNode newnode = new ListNode(value);
    if (head == null) {
      head = newnode;
      tail = newnode;
    } else
      tail = tail.next = newnode;
  }
  static void DeleteFirst() {
    if (head == null)
      System.out.println("There are no nodes in LinkedList");
    else {
      // step1: Creating a Listnode pointer temp , and pointing it to head of LinkedList
      ListNode temp = head;

      // Step2 : Making head->next as head
      head = head.next;

      // Step3 : Making temp's next point to NULL
      temp.next = null;

    }
  }
  public static void main(String args[]) {
    InsertatLast(10);
    InsertatLast(20);
    InsertatLast(30);
    InsertatLast(40);
    System.out.println("LinkedList before Deleting First Node : ");
    PrintList();
    DeleteFirst();
    System.out.println("LinkedList after Deleting First Node : ");
    PrintList();
  }
}

Output:

LinkedList before Deleting First Node :
10–>20–>30–>40–>null
LinkedList after Deleting First Node :
20–>30–>40–>null

Time Complexity:  O(1) Because we are just Manipulating the links, which is a constant operation.

Space Complexity: O(1) Constant Space.

Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article