## 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.