Given a linked list, the task is to remove the last node of the linked list and update the head pointer of the linked list.

Examples:

Input : 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output : 1 -> 2 -> 3 -> 4 -> NULL Input : 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL Output : 2 -> 4 -> 6 -> 8 -> 33 -> NULL

To remove last node, we need to make the next of second last node as NULL and delete memory allocated for last node.

`// CPP program to remove last node of ` `// linked list. ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Function to remove the last node ` ` ` `of the linked list */` `Node* removeLastNode(` `struct` `Node* head) ` `{ ` ` ` `if` `(head == NULL) ` ` ` `return` `NULL; ` ` ` ` ` `if` `(head->next == NULL) ` ` ` `{ ` ` ` `delete` `head; ` ` ` `return` `NULL; ` ` ` `} ` ` ` ` ` `// Find the second last node ` ` ` `Node* second_last = head; ` ` ` `while` `(second_last->next->next != NULL) ` ` ` `second_last = second_last->next; ` ` ` ` ` `// Delete last node ` ` ` `delete` `(second_last->next); ` ` ` ` ` `// Change next of second last ` ` ` `second_last->next = NULL; ` ` ` ` ` `return` `head; ` `} ` ` ` `// Function to push node at 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; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `/* Start with the empty list */` ` ` `Node* head = NULL; ` ` ` ` ` `/* Use push() function to construct ` ` ` `the below list 8 -> 23 -> 11 -> 29 -> 12 */` ` ` `push(&head, 12); ` ` ` `push(&head, 29); ` ` ` `push(&head, 11); ` ` ` `push(&head, 23); ` ` ` `push(&head, 8); ` ` ` ` ` `head = removeLastNode(head); ` ` ` `for` `(Node* temp = head; temp != NULL; temp = temp->next) ` ` ` `cout << temp->data << ` `" "` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8 23 11 29

**Time Complexity :** O(n)

## Recommended Posts:

- Remove every k-th node of the linked list
- Remove first node of the linked list
- Swap Kth node from beginning with Kth node from end in a Linked List
- Linked List | Set 2 (Inserting a node)
- Linked List | Set 3 (Deleting a node)
- Squareroot(n)-th node in a Linked List
- Program for n'th node from the end of a Linked List
- Delete every Kth node from circular linked list
- Insert node into the middle of the linked list
- Reverse each word in a linked list node
- Find first node of loop in a linked list
- Delete a Linked List node at a given position
- Find modular node in a linked list
- Find the fractional (or n/k - th) node in linked list
- Delete a node in a Doubly Linked List

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.