# C++ Program to Delete the First Node in a given Singly Linked List

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields – Data Field and the address of the next node.

Let us assume we have a singly linked list and we need to delete the first node from this linked list. For example,

Input 1 − 4 → 3 → 2 → 1

Output − 3 → 2 → 1 →

Explanation − ‘4’ is the first node in the given singly linked list. After deleting the first node, the linked list will be 3→2→1.

Input 2 − 1 → 2 → 3 →

Output − 2 → 3 →

Explanation − After deleting the first node ‘1’, the linked list will be 2 → 3.

## Approach to solve this problem

Initially, we have a linked list that consists of nodes. Each node contains the data and address to the next node. After inserting the data in the linked list, we will create a function to delete the first node.

Thus, we will create a temporary pointer that initially points to the head and move the head to the next node. Now delete the temporary node and return the linked list.

• A function deleteAthead(node*&head) takes the pointer to the head and deletes the first node of the linked list.

• Create a temporary pointer that initially points to the head.

• Head moves to the next node.

• Delete the temporary pointer.

• Return the linked list.

## Example

#include<iostream>
using namespace std;
int main(){
class node{
public:
int data;
node*next;
node(int d){
data=d;
node*next=NULL;
}
};
void insertAtFirstNode(node*&head, int data){
node*n= new node(data);
}
}
cout<<endl;
}
return;
}
delete temp;
return;
}
int main(){
}
3 → 2 → 1 →