
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete a tail node from the given singly Linked List using C++
A Linked List is a linear Data Structure that contains nodes and each node has two fields; one is the value or data to be inserted and the other field stores the address of the next node.
Our task here is to delete a node from the end of a Linked List. The last node is known as the tail node. If there is no node in the Linked List, then return NULL.
For example −
Input 1 − 1 → 2 → 3 → 4 → 5
Output − 1 → 2 → 3 → 4 →
Explanation − In the given singly linked list, the node from the end is ‘5’. After deleting the last node, the output will be, 1 → 2 → 3 → 4 →.
Input 2 − 5 → 8 →3
Output − 5 → 8 →
Explanation − In the given singly linked list, the node from the end is ‘3’. After deleting the node from the end, the output will be, 5 →8 →.
Approach to solve this problem
The simple approach to solve this particular problem is to create a previous node that later on stores the value of the current node when the current pointer will point to the last node of the linked list.
Iterate over all the nodes of the linked list if the current node points to the last node. And finally, return from the linked list.
Initialize the linked list by inserting the nodes into it.
Function insertAtFirst(node*&head, int data) will insert all the nodes in the linked list.
A function deleteAtTail(node*head) takes a pointer that is currently pointing to the head.
Create a previous Node pointer and initialize it as NULL.
Create a temporary Node pointer that is currently pointing to the head of the pointer.
Traverse the temporary pointer until it does not reach the end of the linked list.
Store the value of the temporary pointer in the previous node pointer.
Delete the temporary pointer.
Return the linked list.
Example
#include<iostream> using namespace std; class node{ public: int data; node*next; node(int d){ data=d; node*next= NULL; } }; void insertAtFirst(node*&head, int data){ node*n= new node(data); n->next= head; head=n; } void printNode(node*head){ while(head!=NULL){ cout<<head->data<<"->"; head=head->next; } cout<<endl; } void deleteatTail(node*head){ node*prev= NULL; node*temp= head; while(temp->next!=NULL){ prev= temp; temp=temp->next; } delete temp; prev->next= NULL; return; } int main(){ node*head= NULL; insertAtFirst(head,5); insertAtFirst(head,4); insertAtFirst(head,3); insertAtFirst(head,2); insertAtFirst(head,1); deleteatTail(head); printNode(head); }
Output
Running the above code will generate the output as,
1→2→3→4→
In the given input singly linked list, 1 → 2 → 3 → 4 → 5, the last node of the linked list is ‘5’. Hence, after deletion of the last node, the linked list will become 1 → 2 → 3 → 4 →.
- Related Articles
- C++ Program to Delete the First Node in a given Singly Linked List
- Find the Kth Node from the end in a given singly Linked List using C++
- Delete all Prime Nodes from a Singly Linked List in C++
- Delete a Linked List node at a given position in C++
- Golang Program to delete the first node from a linked list.
- Golang Program to delete the last node from a linked list.
- Delete all Non-Prime Nodes from a Singly Linked List in C++
- Delete a Doubly Linked List node at a given position in C++
- Delete a Node from linked list without head pointer in C++
- Delete Node in a Linked List in Python
- Search an element in the given singly Linked List using C++
- Write a program in C++ to insert a Node at the beginning of the given Singly linked list
- Python program to delete a node from the end of the Circular Linked List
- Python program to delete a node from the middle of the Circular Linked List
- Program to find the middle node of a singly linked list in Python
