Loop through the whole list and use a double pointer to keep track of the node containing the address of last occurrence node.
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* next;
};
void deleteLast( struct Node** head, int x)
{
struct Node** tmp1 = NULL;
while (*head)
{
if ((*head)->data == x)
{
tmp1 = head;
}
head = &(*head)->next;
}
if (tmp1)
{
struct Node* tmp = *tmp1;
*tmp1 = tmp->next;
free (tmp);
}
}
struct Node* newNode( int x)
{
Node* node = new Node ;
node->data = x;
node->next = NULL;
return node;
}
void display( struct Node* head)
{
struct Node* temp = head;
if (head == NULL)
{
cout << "NULL\n" ;
return ;
}
while (temp != NULL)
{
cout << temp->data << " --> " ;
temp = temp->next;
}
cout << "NULL\n" ;
}
int main()
{
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
head->next->next->next->next->next = newNode(4);
head->next->next->next->next->next->next = newNode(4);
cout << "Created Linked list: " ;
display(head);
deleteLast(&head, 4);
cout << "List after deletion of 4: " ;
display(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void deleteLast( struct Node** head, int x)
{
struct Node** tmp1 = NULL;
while (*head) {
if ((*head)->data == x) {
tmp1 = head;
}
head = &(*head)->next;
}
if (tmp1) {
struct Node* tmp = *tmp1;
*tmp1 = tmp->next;
free (tmp);
}
}
struct Node* newNode( int x)
{
struct Node* node = malloc ( sizeof ( struct Node*));
node->data = x;
node->next = NULL;
return node;
}
void display( struct Node* head)
{
struct Node* temp = head;
if (head == NULL) {
printf ( "NULL\n" );
return ;
}
while (temp != NULL) {
printf ( "%d --> " , temp->data);
temp = temp->next;
}
printf ( "NULL\n" );
}
int main()
{
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
head->next->next->next->next->next = newNode(4);
head->next->next->next->next->next->next = newNode(4);
printf ( "Created Linked list: " );
display(head);
deleteLast(&head, 4);
printf ( "List after deletion of 4: " );
display(head);
return 0;
}
|
Python3
class Node:
def __init__( self , new_data):
self .data = new_data
self . next = None
def deleteLast(head, x):
temp = head
ptr = None
while (temp ! = None ):
if (temp.data = = x):
ptr = temp
temp = temp. next
if (ptr ! = None and ptr. next = = None ):
temp = head
while (temp. next ! = ptr):
temp = temp. next
temp. next = None
if (ptr ! = None and ptr. next ! = None ):
ptr.data = ptr. next .data
temp = ptr. next
ptr. next = ptr. next . next
return head
def newNode(x):
node = Node( 0 )
node.data = x
node. next = None
return node
def display(head):
temp = head
if (head = = None ):
print ( "NULL\n" )
return
while (temp ! = None ):
print ( temp.data, " --> " , end = "")
temp = temp. next
print ( "NULL" )
head = newNode( 1 )
head. next = newNode( 2 )
head. next . next = newNode( 3 )
head. next . next . next = newNode( 4 )
head. next . next . next . next = newNode( 5 )
head. next . next . next . next . next = newNode( 4 )
head. next . next . next . next . next . next = newNode( 4 )
print ( "Created Linked list: " , end = '')
display(head)
head = deleteLast(head, 4 )
print ( "List after deletion of 4: " , end = '')
display(head)
|
Output
Created Linked list: 1 --> 2 --> 3 --> 4 --> 5 --> 4 --> 4 --> NULL
List after deletion of 4: 1 --> 2 --> 3 --> 4 --> 5 --> 4 --> NULL
Given a liked list and a key to be deleted. Delete last occurrence of key from linked. The list may have duplicates.
Examples:
Input: 1->2->3->5->2->10, key = 2
Output: 1->2->3->5->10
The idea is to traverse the linked list from beginning to end. While traversing, keep track of last occurrence key. After traversing the complete list, delete last occurrence by copying data of next node and deleting the next node.
C++
#include <bits/stdc++.h>
struct Node {
int key;
struct Node* next;
};
void deleteLast(Node* head, int key)
{
Node* x = NULL;
Node* temp = head;
while (temp) {
if (temp->key == key)
x = temp;
temp = temp->next;
}
if (x != NULL) {
x->key = x->next->key;
temp = x->next;
x->next = x->next->next;
delete temp;
}
}
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->next = NULL;
return temp;
}
void printList( struct Node* node)
{
while (node != NULL) {
printf ( " %d " , node->key);
node = node->next;
}
}
int main()
{
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(5);
head->next->next->next->next = newNode(2);
head->next->next->next->next->next = newNode(10);
puts ( "Created Linked List: " );
printList(head);
deleteLast(head, 2);
puts ( "\nLinked List after Deletion of 1: " );
printList(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int key;
Node next;
};
static Node deleteLast(Node head, int key)
{
Node x = null ;
Node temp = head;
while (temp != null )
{
if (temp.key == key)
x = temp;
temp = temp.next;
}
if (x != null )
{
x.key = x.next.key;
temp = x.next;
x.next = x.next.next;
}
return head;
}
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.next = null ;
return temp;
}
static void printList( Node node)
{
while (node != null )
{
System.out.printf( " %d " , node.key);
node = node.next;
}
}
public static void main(String args[])
{
Node head = newNode( 1 );
head.next = newNode( 2 );
head.next.next = newNode( 3 );
head.next.next.next = newNode( 5 );
head.next.next.next.next = newNode( 2 );
head.next.next.next.next.next = newNode( 10 );
System.out.printf( "Created Linked List: " );
printList(head);
deleteLast(head, 2 );
System.out.printf( "\nLinked List after Deletion of 1: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def deleteLast(head, key) :
x = None
temp = head
while (temp ! = None ) :
if (temp.key = = key) :
x = temp
temp = temp. next
if (x ! = None ) :
x.key = x. next .key
temp = x. next
x. next = x. next . next
return head
def newNode(key) :
temp = Node( 0 )
temp.key = key
temp. next = None
return temp
def printList( node) :
while (node ! = None ) :
print ( node.key, end = " " )
node = node. next
if __name__ = = '__main__' :
head = newNode( 1 )
head. next = newNode( 2 )
head. next . next = newNode( 3 )
head. next . next . next = newNode( 5 )
head. next . next . next . next = newNode( 2 )
head. next . next . next . next . next = newNode( 10 )
print ( "Created Linked List: " )
printList(head)
deleteLast(head, 2 )
print ( "\nLinked List after Deletion of 1: " )
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int key;
public Node next;
};
static Node deleteLast(Node head, int key)
{
Node x = null ;
Node temp = head;
while (temp != null )
{
if (temp.key == key)
x = temp;
temp = temp.next;
}
if (x != null )
{
x.key = x.next.key;
temp = x.next;
x.next = x.next.next;
}
return head;
}
/// Utility function to create a new node with
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
temp.next = null ;
return temp;
}
static void printList( Node node)
{
while (node != null )
{
Console.Write( " {0} " , node.key);
node = node.next;
}
}
public static void Main(String []args)
{
Node head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(3);
head.next.next.next = newNode(5);
head.next.next.next.next = newNode(2);
head.next.next.next.next.next = newNode(10);
Console.Write( "Created Linked List: " );
printList(head);
deleteLast(head, 2);
Console.Write( "\nLinked List after Deletion of 1: " );
printList(head);
}
}
|
Output:
Created Linked List:
1 2 3 5 2 10
Linked List after Deletion of 1:
1 2 3 5 10
The above solution doesn’t work when the node to be deleted is the last node.
Following solution handles all cases.
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void deleteLast( struct Node* head, int x)
{
struct Node *temp = head, *ptr = NULL;
while (temp) {
if (temp->data == x)
ptr = temp;
temp = temp->next;
}
if (ptr != NULL && ptr->next == NULL) {
temp = head;
while (temp->next != ptr)
temp = temp->next;
temp->next = NULL;
}
if (ptr != NULL && ptr->next != NULL) {
ptr->data = ptr->next->data;
temp = ptr->next;
ptr->next = ptr->next->next;
free (temp);
}
}
struct Node* newNode( int x)
{
struct Node* node = malloc ( sizeof ( struct Node*));
node->data = x;
node->next = NULL;
return node;
}
void display( struct Node* head)
{
struct Node* temp = head;
if (head == NULL) {
printf ( "NULL\n" );
return ;
}
while (temp != NULL) {
printf ( "%d --> " , temp->data);
temp = temp->next;
}
printf ( "NULL\n" );
}
int main()
{
struct Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
head->next->next->next->next->next = newNode(4);
head->next->next->next->next->next->next = newNode(4);
printf ( "Created Linked list: " );
display(head);
deleteLast(head, 4);
printf ( "List after deletion of 4: " );
display(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static void deleteLast(Node head, int x)
{
Node temp = head, ptr = null ;
while (temp!= null )
{
if (temp.data == x)
ptr = temp;
temp = temp.next;
}
if (ptr != null && ptr.next == null )
{
temp = head;
while (temp.next != ptr)
temp = temp.next;
temp.next = null ;
}
if (ptr != null && ptr.next != null )
{
ptr.data = ptr.next.data;
temp = ptr.next;
ptr.next = ptr.next.next;
System.gc();
}
}
static Node newNode( int x)
{
Node node = new Node();
node.data = x;
node.next = null ;
return node;
}
static void display(Node head)
{
Node temp = head;
if (head == null )
{
System.out.print( "null\n" );
return ;
}
while (temp != null )
{
System.out.printf( "%d --> " , temp.data);
temp = temp.next;
}
System.out.print( "null\n" );
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 2 );
head.next.next = newNode( 3 );
head.next.next.next = newNode( 4 );
head.next.next.next.next = newNode( 5 );
head.next.next.next.next.next = newNode( 4 );
head.next.next.next.next.next.next = newNode( 4 );
System.out.print( "Created Linked list: " );
display(head);
deleteLast(head, 4 );
System.out.print( "List after deletion of 4: " );
display(head);
}
}
|
Python3
class Node:
def __init__( self , new_data):
self .data = new_data
self . next = None
def deleteLast(head, x):
temp = head
ptr = None
while (temp! = None ):
if (temp.data = = x) :
ptr = temp
temp = temp. next
if (ptr ! = None and ptr. next = = None ):
temp = head
while (temp. next ! = ptr) :
temp = temp. next
temp. next = None
if (ptr ! = None and ptr. next ! = None ):
ptr.data = ptr. next .data
temp = ptr. next
ptr. next = ptr. next . next
return head
def newNode(x):
node = Node( 0 )
node.data = x
node. next = None
return node
def display( head):
temp = head
if (head = = None ):
print ( "None\n" )
return
while (temp ! = None ):
print ( temp.data, " -> " ,end = "")
temp = temp. next
print ( "None" )
head = newNode( 1 )
head. next = newNode( 2 )
head. next . next = newNode( 3 )
head. next . next . next = newNode( 4 )
head. next . next . next . next = newNode( 5 )
head. next . next . next . next . next = newNode( 4 )
head. next . next . next . next . next . next = newNode( 4 )
print ( "Created Linked list: " )
display(head)
head = deleteLast(head, 4 )
print ( "List after deletion of 4: " )
display(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static void deleteLast(Node head, int x)
{
Node temp = head, ptr = null ;
while (temp != null )
{
if (temp.data == x)
ptr = temp;
temp = temp.next;
}
if (ptr != null && ptr.next == null )
{
temp = head;
while (temp.next != ptr)
temp = temp.next;
temp.next = null ;
}
if (ptr != null && ptr.next != null )
{
ptr.data = ptr.next.data;
temp = ptr.next;
ptr.next = ptr.next.next;
}
}
static Node newNode( int x)
{
Node node = new Node();
node.data = x;
node.next = null ;
return node;
}
static void display(Node head)
{
Node temp = head;
if (head == null )
{
Console.Write( "null\n" );
return ;
}
while (temp != null )
{
Console.Write( "{0} --> " , temp.data);
temp = temp.next;
}
Console.Write( "null\n" );
}
public static void Main(String[] args)
{
Node head = newNode(1);
head.next = newNode(2);
head.next.next = newNode(3);
head.next.next.next = newNode(4);
head.next.next.next.next = newNode(5);
head.next.next.next.next.next = newNode(4);
head.next.next.next.next.next.next = newNode(4);
Console.Write( "Created Linked list: " );
display(head);
deleteLast(head, 4);
Console.Write( "List after deletion of 4: " );
display(head);
}
}
|
Output:
Created Linked List:
1 2 3 4 5 4 4
Linked List after Deletion of 1:
1 2 3 4 5 4
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.