You are given an unsorted singly linked list that may contain duplicate elements. You are supposed to remove all duplicate elements from the linked list and keep only the last appearance of elements.

```
#include<bits/stdc++.h>
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
Node *lastAppearance(Node *head){
// Write your code here.
if(head == NULL) return NULL;
Node* prev = NULL;
Node* curr = head;
Node* next = head->next;
while(curr != NULL){
next =curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
Node* newprev = NULL;
Node* newHead = prev;
map<int, int> m;
while(newHead != NULL){
if(m[newHead->data] == 0){
m[newHead->data]++;
newprev = newHead;
newHead = newHead->next;
}else{
newprev->next = newHead->next;
newHead = newHead->next;
}
}
curr = prev;
next = curr->next;
prev = NULL;
while(curr != NULL){
next =curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
```

My Approach:

I reversed the linked list and then removed duplicates using hashing. At last, I reversed the list again. But I am getting TLE for this, N<=5X1e5