Deletion from beginning in singly linked list in C++

How to delete element from beginning in Singly Linked List in C++?

Deletion from beginning in singly linked list in C++ is one of the simplest operation performed on linked list. We can perform following deletion operation on the linked list:-

  • Deleting from beginning of the list
  • Deletion from End of the list
  • Deleting a specific node

In this article we will learn how we can delete the head of the list. This article also consist of steps, algorithm and a program coded in C++ to understand the concepts well.

delete

Defining a singly linked list in C++

Nodes of singly linked list is created by using the code mentioned besides.
This set of code will construct linked list by creating each node of the list.

class Node  
{   
    int data;  
    Node *next;  
};
Singly Linked List Deletion from beginning in C++
Linked List Deletion at the Start in C++

Program for deletion from beginning in singly linked list in C++

This method uses non-member class functions and head needs to be passed seperately

Run
#include <iostream>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
};

void insertStart(Node** head, int data){

    Node* new_node = new Node();

    // assign data value
    new_node->data = data;
    // change the next node of this new_node 
    // to current head of Linked List
    new_node->next = *head;

    //changing the new head to this newly entered node
    *head = new_node;

}
void display(Node* node){

    //as linked list will end when Node is Null
    while(node!=NULL){
        cout << node->data << " "; node = node->next;
    }
    cout << endl;
}

int main(){

    Node* head = NULL;

    insertStart(&head,10);
    insertStart(&head,11);
    insertStart(&head,12);
    insertStart(&head,13);
    insertStart(&head,14);
    insertStart(&head,15);
    insertStart(&head,16);
    insertStart(&head,17);
    insertStart(&head,18);

    display(head); 
    return 0;  
}

Output

18 17 16 15 14 13 12 11 10 

This method uses member class functions and head need not to be passed separately as its member variable of class and can be accessed.

Run
#include <iostream>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
};

class LinkedList
{
    private:
        Node* head;
    public:
        LinkedList() { // constructor
        head = NULL;
    }
        void insertBeginning(int data);
        void display();
};

void LinkedList::insertBeginning(int data){

    Node* new_node = new Node();
    
    // assign data value
    new_node->data = data;
    // change the next node of this new_node 
    // to current head of Linked List
    new_node->next = head;

    //changing the new head to this freshly entered node
    head = new_node;

}
void LinkedList::display(){
    
    Node* node = new Node();
    node = head;
    
    //as linked list will end when Node is Null
    while(node!=NULL){
        cout << node->data << " "; node = node->next;
    }
    cout << endl; 
} 

int main()
{ 
    LinkedList* singly_list = new LinkedList(); 
    singly_list->insertBeginning(10);
    singly_list->insertBeginning(11);
    singly_list->insertBeginning(12);
    singly_list->insertBeginning(13);
    singly_list->insertBeginning(14);
    singly_list->insertBeginning(15);
    singly_list->insertBeginning(16);
    singly_list->insertBeginning(17);
    singly_list->insertBeginning(18);

    singly_list->display(); 
    
    return 0;  
}

Output

18 17 16 15 14 13 12 11 10