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
Program for deletion from beginning in singly linked list in C++
Method 1
Method 2
Method 1
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
Method 2
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java
Login/Signup to comment