Deletion at end (Removal of last node) in a Linked List (original) (raw)

Last Updated : 21 Oct, 2025

Given the head of a linked list, delete the last node of the given linked list.

**Examples:

**Input:

blobid2_1755947119

**Output:

blobid3_1755947241

**Explanation: The last node of the linked list is 5, so 5 is deleted.

**Input:

blobid0_1755945539

**Output: 3 -> 12 -> NULL

blobid1_1755945555

**Explanation: The last node of the linked list is 15, so 15 is deleted.

Try It Yourselfredirect icon

**Approach:

To perform the deletion operation at the end of linked list, we need to traverse the list to find the **second last node, then set its next pointer to **null. If the list is empty then there is no node to delete or has only one node then point **head to **null.

**Step-by-step approach:

**Implementation:

C++ `

#include using namespace std;

// Node class for the linked list class Node { public: int data; Node* next;

Node(int x) {
    this->data = x;
    this->next = nullptr;
}

};

// Function to remove the last node // of the linked list Node* removeLastNode(Node* head) { // If the list is empty, return nullptr if (head == nullptr) { return nullptr; }

// If the list has only one node, delete it and return nullptr
if (head->next == nullptr) {
    delete head;
    return nullptr;
}

// Find the second last node
Node* secondLast = head;
while (secondLast->next->next != nullptr) {
    secondLast = secondLast->next;
}

// Delete the last node
delete secondLast->next;

// Change next of second last
secondLast->next = nullptr;

return head;

}

void printList(Node* head) { while (head != nullptr) { cout << head->data << " -> "; head = head->next; } cout << "nullptr" << endl; }

int main() { // Creating a static linked list // 1 -> 2 -> 3 -> 4 -> 5 -> nullptr Node* head = new Node(1); head->next = new Node(2); head->next->next = new Node(3); head->next->next->next = new Node(4); head->next->next->next->next = new Node(5);

// Removing the last node
head = removeLastNode(head);

printList(head);

return 0;

}

C

#include <stdio.h> #include <stdlib.h>

// Node structure for the linked list struct Node { int data; struct Node *next; };

// Function to create a new node struct Node *createNode(int x) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->data = x; newNode->next = NULL; return newNode; }

// Function to remove the last node of the linked list struct Node *removeLastNode(struct Node *head) { // If the list is empty if (head == NULL) return NULL;

// If the list has only one node
if (head->next == NULL)
{
    free(head);
    return NULL;
}

// Find the second last node
struct Node *secondLast = head;
while (secondLast->next->next != NULL)
{
    secondLast = secondLast->next;
}

// Delete the last node
free(secondLast->next);

// Update next of second last node
secondLast->next = NULL;

return head;

}

// Function to print the linked list void printList(struct Node *head) { while (head != NULL) { printf("%d", head->data); if (head->next != NULL) printf(" -> "); head = head->next; } printf(" -> NULL\n"); }

int main() { // Creating the linked list: 1 -> 2 -> 3 -> 4 -> 5 struct Node *head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); head->next->next->next->next = createNode(5);

// Remove the last node
head = removeLastNode(head);

// Print the updated list
printList(head);

return 0;

}

Java

class GfG {

// Node class for the linked list
static class Node {
    int data;
    Node next;

    Node(int x) {
        this.data = x;
        this.next = null;
    }
}

// Function to remove the last 
// node of the linked list
static Node removeLastNode(Node head) {
    // If the list is empty, return null
    if (head == null) {
        return null;
    }

    // If the list has only one node, 
    // delete it and return null
    if (head.next == null) {
        return null;
    }

    // Find the second last node
    Node secondLast = head;
    while (secondLast.next.next != null) {
        secondLast = secondLast.next;
    }

    // Delete the last node by making 
    // secondLast point to null
    secondLast.next = null;

    return head;
}

static void printList(Node head) {
    while (head != null) {
        System.out.print(head.data + " -> ");
        head = head.next;
    }
    System.out.println("null");
}

public static void main(String[] args) {
    // Creating a static linked list
    // 1 -> 2 -> 3 -> 4 -> 5 -> null
    Node head = new Node(1);
    head.next = new Node(2);
    head.next.next = new Node(3);
    head.next.next.next = new Node(4);
    head.next.next.next.next = new Node(5);

    // Removing the last node
    head = removeLastNode(head);

    printList(head);
}

}

Python

Node class for the linked list

class Node: def init(self, x): self.data = x self.next = None

Function to remove the last

node of the linked list

def removeLastNode(head): # If the list is empty, return None if head is None: return None

# If the list has only one node, 
# delete it and return None
if head.next is None:
    return None

# Find the second last node
secondLast = head
while secondLast.next.next is not None:
    secondLast = secondLast.next

# Delete the last node by making 
# second_last point to None
secondLast.next = None

return head

def printList(head): while head is not None: print(f"{head.data} -> ", end="") head = head.next print("None")

if name == "main": # Creating a static linked list # 1 -> 2 -> 3 -> 4 -> 5 -> None head = Node(1) head.next = Node(2) head.next.next = Node(3) head.next.next.next = Node(4) head.next.next.next.next = Node(5)

# Removing the last node
head = removeLastNode(head)

printList(head)

C#

using System;

// Node class for the linked list class Node { public int data; public Node next;

public Node(int data) {
    this.data = data;
    this.next = null;
}

}

class GfG { // Function to remove the last // node of the linked list public static Node removeLastNode(Node head) { // If the list is empty, return null if (head == null) { return null; }

    // If the list has only one node, return null
    if (head.next == null) {
        return null;
    }

    // Find the second last node
    Node secondLast = head;
    while (secondLast.next.next != null) {
        secondLast = secondLast.next;
    }

    // Delete the last node by making 
    // secondLast point to null
    secondLast.next = null;

    return head;
}

public static void printList(Node head) {
    while (head != null) {
        Console.Write(head.data + " -> ");
        head = head.next;
    }
    Console.WriteLine("null");
}

static void Main() {
    // Creating a static linked list
    // 1 -> 2 -> 3 -> 4 -> 5 -> null
    Node head = new Node(1);
    head.next = new Node(2);
    head.next.next = new Node(3);
    head.next.next.next = new Node(4);
    head.next.next.next.next = new Node(5);

    // Removing the last node
    head = removeLastNode(head);

    printList(head);
}

}

JavaScript

// Node class for the linked list class Node { constructor(data) { this.data = data; this.next = null; } }

// Function to remove the last // node of the linked list function removeLastNode(head) { // If the list is empty, return null if (head === null) { return null; }

// If the list has only one 
// node, return null
if (head.next === null) {
    return null;
}

// Find the second last node
let secondLast = head;
while (secondLast.next.next !== null) {
    secondLast = secondLast.next;
}

// Delete the last node by disconnecting it
secondLast.next = null;

return head;

}

function printList(head) { let current = head; while (current !== null) { process.stdout.write(current.data + " -> "); current = current.next; } console.log("null"); }

// Driver code function main() { // Creating a static linked list // 1 -> 2 -> 3 -> 4 -> 5 -> null let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5);

// Removing the last node
head = removeLastNode(head);

printList(head);

}

main();

`

Output

1 -> 2 -> 3 -> 4 -> nullptr

**Time Complexity: O(n), traversal of the linked list till its end, so the time complexity required is _O(n).
**Auxiliary Space: O(1)