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 most fundamental operations in linked list manipulation. This operation is typically used when we want to remove the first element (head node) from the list. Understanding how to perform this operation is essential for mastering data structures in C++. 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
Deleting from beginning of the list

Defining a singly linked list in C++

Deletion from beginning in singly linked list in C++ – A singly linked list is a fundamental data structure consisting of nodes where each node contains data and a pointer to the next node in the sequence. Nodes of a singly linked list are created by using the code mentioned alongside. This set of code constructs the linked list by dynamically creating each node and linking them sequentially.

  • 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++

Program for deleting a node from beginning in singly linked list in C++

  • Start with the head pointer of the linked list.
  • Check if the list is empty (head == NULL).
    • If empty, print a message like “List is empty” and exit.
  • If not empty:
    • Store the current head node in a temporary pointer (temp = head).
    • Move the head pointer to the next node (head = head->next).
    • Delete the node pointed by temp using delete temp.
  • The node at the beginning is now deleted, and the list starts from the new head.

Advantages and Disadvantages of deleting a node from the beginning in a singly linked list in C++

Advantages Disadvantages 
Simple and easy to implementCannot delete a node from the middle or end directly
Deletion operation is very fast (O(1))If the list is empty, deletion is not possible (needs a check)
No need to traverse the list to deleteOnly removes the first node, no control over other nodes
Efficient for stack-like structures (LIFO)If head pointer is not updated correctly, list may become corrupted
Requires only updating the head pointerMemory leak risk if the deleted node is not properly deleted (free/delete)
Useful when oldest data is at the frontFrequent deletions from beginning can lead to fragmentation or overhead

Wrap It Up

Deleting from beginning of single linked list in C++, the first node in a singly linked list is a simple yet powerful operation that’s fundamental to understanding how linked lists work. It’s commonly used in data structures like stacks because of how efficient it is requiring just a few steps to execute.

  • Fast and efficient: The operation runs in constant time, O(1), which means it’s always quick, no matter the size of the list.
  • Update the head pointer: You simply move the head pointer to the next node in the list.
  • Memory management matters: Always free the memory of the removed node to avoid memory leaks.

FAQs

If the list is empty (head is NULL), deletion should be skipped after checking, otherwise it may cause a segmentation fault.

Only if nodes are statically allocated, but in most practical cases, dynamic memory is used and delete must be called to free memory.

Once deleted using delete, the memory is freed; restoring it is not possible unless it was stored beforehand.

Always store the current head in a temporary pointer, move the head to the next node, and then delete the temporary node.

No, deleting from the beginning of a singly linked list is not thread-safe in general. If multiple threads access or modify the list concurrently, you should use proper synchronization (like mutexes) to avoid race conditions.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription