• Register
2 votes


Hey Kodlogs,

I hope all of you are doing good. Well, I am struggling with a problem, regarding C++ programming. I am trying to write codes that should delete the last node in the linked list. For example, if I give an input of

1 -> 2 -> 3 -> 4 -> 5 -> NULL

The output should be

1 -> 2 -> 3 -> 4 -> NULL

Perhaps, I miscalculated the algorithm. So, the question is simple. How can I delete the last node in the linked list? Please, solve my problem with the legit algorithm.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes


Well, to delete the last node of a linked list, we have to find the second last node and make the next pointer of that node null. The algorithm behind it is:

# Create a Data/Node list

# If the program finds the first node null or there is only one node, then return null

# Create extra space and let in go the linked list until the second last node.

# Delete the last node.

Let’s write the program now.

#include <iostream> 
using namespace std;   
struct Node { 
    int data; 
    struct Node* next; 

Node* lastNodeRemove(struct Node* head){ 
    if (head == NULL) 
        return NULL; 
    if (head->next == NULL) { 
        delete head; 
        return NULL; 
    Node* second_last = head; 
    while (second_last->next->next != NULL) 
        second_last = second_last->next;   
    delete (second_last->next);   
    second_last->next = NULL; 
    return head; 
void push(struct Node** head_ref, int new_data){ 
    struct Node* new_node = new Node; 
    new_node->data = new_data; 
    new_node->next = (*head_ref); 
    (*head_ref) = new_node; 
int main(){
    push(&head, 5); 
    push(&head, 4); 
    push(&head, 3); 
    push(&head, 2); 
    push(&head, 1); 
    head = lastNodeRemove(head); 
    for (Node* temp = head; temp != NULL; temp = temp->next) 
        cout << temp->data << " "; 
    return 0; 

This above program should print what you are looking for. Thanks.

13 9 6
94,240 points

Related questions

0 votes
1 answer 9 views
I require a program in C that will help me to insert a node at the end in linked list.
asked Oct 5, 2020 Daniel Anderson 4k points
0 votes
1 answer 24 views
Problem: I'm doing insertion ordering, so I want to print each change that is made inside the array Lista1in the function OrdenarPorIncersion, but I can't do it because if I put the function MostrameEstaShitinside the for loop, the loop only executes once when it iis 1but already ... prints the first changes when i is 1 and the entire for loop is skipped. } } void ShowMeThisShit (int List1 []) {
asked Dec 4, 2020 sasha 13.2k points
0 votes
1 answer 45 views
I have this program to work with a doubly-linked list in C ++. What goes wrong is the end delete function, which as its name implies would have to remove the end node from the list with its contents, but instead it does display memory addresses. How can I fix the error? #include <iostream> #include ... CAB) { cout << endl << "" <<AUX-> n << "was removed from the list"; CAB = NULL; } getchar (); }
asked Aug 6, 2020 amna 97.2k points
0 votes
1 answer 8 views
Program Hey, when I free up memory generally it doesn&rsquo;t crash. If/else covers everything. It was started with the deleting the second item if there is present this what while loop does there is only one present that is delete starNode starNode is a pointer that always ... ; delete current; current = soon; } else { delete current; break; } } } delete this->startNode; delete this->endNode; }
asked Dec 22, 2020 chris jordan 2.4k points
0 votes
1 answer 32 views
Problem: I wrote a simple function to insert at the end of a linked list in C ++, but in the end it only shows the first data. I can't figure out what happened. This is the function: void InsertAtEnd (node * & firstNode, string name) {          node * temp = firstNode;       ...          temp-> next = NULL;        if (firstNode == NULL) firstNode = temp; } I will be great if you&rsquo;ll help me.
asked Nov 8, 2020 sasha 13.2k points
0 votes
1 answer 11 views
Problem: how to Insert node into the middle of the linked list
asked Feb 13 jasimchaudary 640 points
0 votes
1 answer 9 views
Problem: What is the correct way to delete a node in a link list?How can we delete it?Please provide compelete code.
asked Nov 3, 2020 Mubashi-Question 1.7k points
0 votes
1 answer 3 views
Problem: Insert node at a certain position in a linked list C++.
asked Feb 26 Ashty 1.8k points
0 votes
1 answer 39 views
I&rsquo;m not an expert in C++ and hoping for some help here in this issue.
asked Dec 7, 2020 TeamScript 13.5k points
0 votes
1 answer 18 views
Problem: I've created a linked list template class which contains a copy constructor that performs a deep copy of the list. LinkedList.hpp template <class T> class LinkedList { protected: struct Node { T value; Node *next; Node(T val, Node *nxt=NULL) { value = val; next = nxt; } }; Node *head; public: LinkedList(); LinkedList(LinkedList &obj); ~LinkedList(); };
asked Dec 23, 2020 Mashhoodch 10.2k points