Skip to content

Delete first node in linked list: Program and Algorithm

Delete first node in linked list

In this article, you’ll learn how to delete first node in linked list in C++, along with that you’ll also see the algorithm to delete first node in linked list.

So, Let’s get started!

Prerequisite: You should have the knowledge of How do you create a node in a linked list? and How to traverse a singly linked list?

How do you delete first node in linked list?

Let’s see how you can delete first node in linked list step-by-step:

Variables we are using here:

  1. head : A pointer of Node type pointing to the first node of the linked list.
  2. temp: A pointer of Node type which is also pointing to the first node (i.e., head).

The following algorithm deletes first node in linked list:

  • Step 01: Start
  • Step 02: If head == NULL
    • Step 03: Return NULL
    • Step 04: Go to step 11
  • Step 05: [End of If ]
  • Step 06: Create a new node named temp of Node type and initialize it with head.
  • Step 07: Set head = head -> next
  • Step 08: Set temp -> next = NULL
  • Step 09: Delete temp
  • Step 10: Return head
  • Step 11: Stop

Visual Representation:

Explanation of the above algorithm:

So, If you see the line number 78 of the below code, you’ll find, we are defining a function named as deleteFirstNode.

And, in that function (i.e., deleteFirstNode function), we are passing the head pointer of the linked list as a parameter whose first node we wish to delete.

The return type of this deleteFirstNode function is a pointer of Node type.

Yup! I know, I know…

You might be wondering, why the return type of this deleteFirstNode function is a pointer of Node type here. Right?

It’s only because, after deleting the first node of this linked list we will return the head of that list which will now point to the second node of the same linked list. As the first node will have been deleted.

Now, inside the function, we are checking a condition, whether the linked list is empty or not.

So, if the linked list is empty which means, the head pointer is pointing to NULL (i.e., head == NULL), we don’t need to delete anything. Right?

Because, if the list is already empty then what is left to delete. ?

After that, in the next statement we are creating a pointer of node type named temp which is pointing to the first node of the linked list.

Now, your question would be, Why we are creating this pointer? Right?

We are creating this temp pointer because, as you already know, we have to delete the first node of this list.

So, to access or to keep track of this first node we must assign the address of this first node to some variable. which will help to delete this node.

Because, if you’ll not delete the first node, it will be somewhere in the memory without any use.

So, it is better to delete this node or free the memory acquired by it.

Now, we have to move our head pointer towards right so that it can point to the second node of the list.

Why this step is required ?

Because, here we can see that the first node has to be deleted. and, if head is still pointing to the first node then it does not make any sense. Right?

Here, the head pointer must point to the second node of the list, because, after deleting the first node the second node becomes the first node of the updated list.

And, that’s why, head must always point to the second node after deleting the first node.

After that, before deleting the temp which is a pointer to the first node, we have to make sure that the temp must point to NULL.

Because, now it will not point to any node.

So, it is better to point to NULL to avoid any type of error.

And, at last we are releasing or free the memory allocated to the temp pointer with the help of destructor (which we have defined at the beginning of the code).

Simply by writing the delete keyword followed by the name of the pointer which we wish to delete.

And, then we are returning the head pointer of new updated linked list.

That’s it…

Now, we are ready to go.

So, let’s dive into the actual coding part…

Implementation in C++:

// writing a program in C++ to delete first node of a linked list

#include<iostream>
using namespace std;

// creating a class "Node"
class Node
{
    public:
            int data;
            Node *next;

    //constructor
    Node(int data)
    {
        this -> data = data;
        this -> next = NULL;
    }

    //destructor
    ~Node()
    {
        if(this -> next != NULL)
        {
            delete next;
            this -> next = NULL;
        }
    }
};


// Traversing a linked list
void TraverseList(Node* &head)
{
    if(head == NULL)
        cout << "List is empty" << endl;
    else
    {
        Node* temp = head;

        while(temp != NULL)
        {
            cout << temp -> data << " -> " ;
            temp = temp -> next;
        }
        cout << "Null" << endl;
    }
}


// function to insert a node at the tail of a linked list

void insertAtTail(Node*  &head, int data)
{
    //creation of a new node
    Node* tail = new Node(data);

    //empty list
    if(head == NULL)

        head = tail;

    else
    {
        Node* temp = head;

        while(temp -> next != NULL)
        {
            temp = temp -> next;
        }
        temp -> next = tail;
    }

}

// function to delete first node of a linked list

Node* deleteFirstNode(Node* head)
{
    if(head == NULL)
        return NULL;

    Node* temp = head;
    head = head -> next;

    temp -> next = NULL;
    delete temp;

    return head;
}


//Driver's code

int main()
{
    // if list is not empty

    Node* node1 = new Node(3);
    Node* head = node1;

    insertAtTail(head, 11);
    insertAtTail(head, 27);
    insertAtTail(head, 99);
    head = deleteFirstNode(head);


    TraverseList(head);

    return 0;


}


If you compile and run the above program, it will produce the following result:

Output:

// before deletion of the first node (when we didn't call the deleteFirstNode() function)

3 -> 11 -> 27 -> 99 -> Null

// after deletion of the first node (when we call the deleteFirstNode() function)

11 -> 27 -> 99 -> Null



Conclusion:

In this article, you learned how to delete first node of a linked list in C++, you also became familiar with the algorithm to delete the first node of a linked list.

So,

I really hope that you liked my article and got your answer.

Now, It’s your turn!

If you have any question, feel free to ask in the comment section below right now.

And, and, and…

Don’t forget to share this article to your friends.

Coz, as you know, Sharing is Caring! ?… Right?


Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *