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
Explanation: The last node of the linked list
is 5, so 5 is deleted.
Input: 2 -> 4 -> 6 -> 8 -> 33 -> 67 -> NULL
Output: 2 -> 4 -> 6 -> 8 -> 33 -> NULL
Explanation: The last node of the linked list
is 67, so 67 is deleted.
Approach: To delete the last node of a linked list, find the second last node and make the next pointer of that node null.

Algorithm:
- If the first node is null or there is only one node, then they return null.
- if headNode == null then return null
- if headNode.nextNode == null then free
- head and return null
- Create an extra space secondLast, and traverse the linked list till the second last node.
- while secondLast.nextNode.nextNode != null
secondLast = secondLast.nextNode
- delete the last node, i.e. the next node of the second last node delete(secondLast.nextNode), and set the value of the next second-last node to null.
Implementation:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* removeLastNode( 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()
{
Node* head = NULL;
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;
}
|
Java
class GFG {
static class Node {
int data;
Node next;
};
static Node removeLastNode(Node head)
{
if (head == null )
return null ;
if (head.next == null ) {
return null ;
}
Node second_last = head;
while (second_last.next.next != null )
second_last = second_last.next;
second_last.next = null ;
return head;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 12 );
head = push(head, 29 );
head = push(head, 11 );
head = push(head, 23 );
head = push(head, 8 );
head = removeLastNode(head);
for (Node temp = head; temp != null ; temp = temp.next)
System.out.print(temp.data + " " );
}
}
|
Python3
import sys
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head, data):
if not head:
return Node(data)
temp = Node(data)
temp. next = head
head = temp
return head
def removeLastNode(head):
if head = = None :
return None
if head. next = = None :
head = None
return None
second_last = head
while (second_last. next . next ):
second_last = second_last. next
second_last. next = None
return head
if __name__ = = '__main__' :
head = None
head = push(head, 12 )
head = push(head, 29 )
head = push(head, 11 )
head = push(head, 23 )
head = push(head, 8 )
head = removeLastNode(head)
while (head):
print ( "{} " . format (head.data), end = "")
head = head. next
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node removeLastNode(Node head)
{
if (head == null )
return null ;
if (head.next == null ) {
return null ;
}
Node second_last = head;
while (second_last.next.next != null )
second_last = second_last.next;
second_last.next = null ;
return head;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
head = removeLastNode(head);
for (Node temp = head; temp != null ; temp = temp.next)
Console.Write(temp.data + " " );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function removeLastNode(head)
{
if (head == null )
return null ;
if (head.next == null ) {
return null ;
}
var second_last = head;
while (second_last.next.next != null )
second_last = second_last.next;
second_last.next = null ;
return head;
}
function push(head_ref , new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
var head = null ;
head = push(head, 12);
head = push(head, 29);
head = push(head, 11);
head = push(head, 23);
head = push(head, 8);
head = removeLastNode(head);
for (temp = head; temp != null ; temp = temp.next)
document.write(temp.data + " " );
</script>
|
Complexity Analysis:
- Time Complexity: O(n).
The algorithm involves traversal of the linked list till its end, so the time complexity required is O(n).
- Space Complexity: O(1).
No extra space is required, so the space complexity is constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
11 Jan, 2023
Like Article
Save Article