# Delete last node in linked list c++

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.

## 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;
};

return NULL;

return NULL;
}
while (second_last->next->next != NULL)
second_last = second_last->next;
delete (second_last->next);
second_last->next = NULL;
}

void push(struct Node** head_ref, int new_data){
struct Node* new_node = new Node;
new_node->data = new_data;
}

int main(){
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.

## Related questions

14 views
I require a program in C that will help me to insert a node at the end in linked list.
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 []) {
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; }
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!
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 (); }
4 views
Problem: Please let me the way to delete a node from linked list in c .
4 views
Problem: Is there anyone to helo on > 'delete node in linked list c++' ?