w3resource

C++ Linked List Exercises: Delete the last node of a Singly Linked List

C++ Linked List: Exercise-12 with Solution

Write a C++ program to delete the last node of a Singly Linked List.

Test Data:
Original list:
7 5 3 1
Remove the last node of the said list:
Updated list:
7 5 3
Again remove the last node of the said list:
Updated list:
7 5

Sample Solution:

C++ Code:

#include <iostream>

using namespace std;

struct  Node
{   
    int num;
    Node *next;
 }; //node constructed

  int size = 0;
  void insert(Node** head, int num){
    Node* new_Node = new Node();
    new_Node->num = num;
    new_Node->next = *head;
    *head = new_Node;    
     size++;
}

Node* delete_last_node(struct Node* head)
{
    if (head == NULL)
        return NULL;
 
    if (head->next == NULL) {
        delete head;
        return NULL;
    }
 
    // Locate the second last node
    Node* second_last_node = head;
    while (second_last_node->next->next != NULL)
        second_last_node = second_last_node->next;
 
    // Remove the last node from the list
    delete (second_last_node->next);
 
    // Change the next to the second last  
    second_last_node->next = NULL;
 
    return head;
}

  //Display all nodes
  void display_all_nodes(Node* node)
    { 
      while(node!=NULL){
        cout << node->num << " "; 
        node = node->next; 
    } 
}

int main()
{
    Node* head = NULL;
    insert(&head,1);
    insert(&head,3);
    insert(&head,5);
    insert(&head,7);
    cout << "Original list:\n";
    display_all_nodes(head); 
    cout << "\n\nRemove the last node of the said list:";
    cout << "\nUpdated list:\n";
    head = delete_last_node(head);
    display_all_nodes(head); 
    cout << "\n\nAgain remove the last node of the said list:";
    cout << "\nUpdated list:\n";
    head = delete_last_node(head);
    display_all_nodes(head); 
    cout<<endl;
    return 0;
}

Sample Output:

Original list:
7 5 3 1

Remove the last node of the said list:
Updated list:
7 5 3

Again remove the last node of the said list:
Updated list:
7 5

Flowchart:

Flowchart: Delete the last node of a Singly Linked List.
Flowchart: Delete the last node of a Singly Linked List.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Delete a middle node from a Singly Linked List.
Next C++ Exercise: Delete the nth node of a Singly Linked List from end.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

How to Use CCache with CMake?

This directory contains loads of symlinks for every possible name the compiler could be called from (like gcc and gcc-4.3), all pointing to ccache.

And I didn't even create the symlinks. That directory comes pre-filled when I install ccache on Debian.

Ref: https://bit.ly/3KoJN3g

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook