• Register
2 votes
36 views

Problem:

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

Solution:

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,260 points

Related questions

0 votes
1 answer 14 views
14 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
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 16.2k points
0 votes
1 answer 34 views
34 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 4 views
4 views
Problem: how to I Solve this: remove nth node from end of list HELP! I need help understanding this: remove nth node from end of list Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 10 Mashhoodch 13k points
0 votes
1 answer 57 views
57 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 111k points
0 votes
1 answer 4 views
4 views
Problem: Please let me the way to delete a node from linked list in c .
asked Apr 27 salim1 60.6k points
0 votes
1 answer 4 views
4 views
Problem: Is there anyone to helo on > 'delete node in linked list c++' ?
asked Mar 17 sadi1982 36.3k points
0 votes
1 answer 5 views
5 views
Problem: How to insert node at a certain position in a linked list C++?
asked Apr 14 Humaira ahmed 50.7k points
0 votes
1 answer 33 views
33 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 16.2k points
0 votes
1 answer 8 views
8 views
Problem: I will request someone to answer my question Please, I have searched on the web but I got no useful information for this, now I am in difficulty continuing my learning
asked Mar 21 shamimkhan 15.1k points