
- 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
Remove Last Node of the Linked List using C++
We are provided with a singly linked list, and we are tasked to remove the last node from that list. In this problem, we are simply going to traverse through the given list and simply remove the last node.
Approach to find The Solution
In this approach, we go through the given list, and we keep track of the previous node and the current node. Now when our current node becomes the last node, we change previous -> next to NULL and delete the current node.
Example
#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; void push(struct Node** ref, int new_data) { // pushing the node struct Node* new_n = new Node; new_n->data = new_data; new_n->next = (*ref); (*ref) = new_n; } int main() { Node* head = NULL; push(&head, 12); push(&head, 29); push(&head, 11); push(&head, 23); push(&head, 8); auto curr = head, prev = head; if (!curr || !curr -> next) // if list only has one element or the list is empty cout << "Empty\n"; else { while (curr) { // while curr != NULL if (!curr -> next) { prev -> next = NULL; delete(curr); // freeing the space break; } prev = curr; curr = curr -> next; // moving to the next node } } for (Node* temp = head; temp != NULL; temp = temp->next) // printing the data cout << temp->data << " "; return 0; }
Output
8 23 11 29
Explanation of the above code
In this approach, we traverse through the array, keeping track of the current node and the previous node. Now when our current node becomes the last node, we change previous -> next to NULL and delete the current node. The overall time complexity of the given program is O(N), where N is the size of our given list.
Time Complexity − O(N)
N: The size of our array
Conclusion
In this article, we solve a problem to Remove the last node from the given linked list. We also learned the C++ program for this problem and the complete approach we solved. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.
- Related Articles
- Remove First Node of the Linked List using C++
- Remove Every K-th Node Of The Linked List
- Golang Program to delete the last node from a linked list.
- Python Program to Print Nth Node from the last of a Linked List
- C# Program to remove a node at the end of the Linked List
- Golang Program to update the last node value in a linked list.
- C# Program to remove a node at the beginning of a Linked List
- Program to find the K-th last node of a linked list in Python
- Find the second last node of a linked list in single traversal in C++
- C# program to remove the first occurrence of a node in a Linked List
- C# Program to add a node at the last position in a Linked List
- Linked List Random Node in C++
- Remove elements from a linked list using Javascript
- Delete a tail node from the given singly Linked List using C++
- Golang program to insert a node at the ith index node, when the index is at the last position in the linked list.
