Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Remove_Duplicates_from_Unsorted_Doubly_LL.c++ #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <bits/stdc++.h>
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
*****************************************************************/

// Problem Link-> https://shorturl.at/15bsG
/* Complexity Analysis:-
Time Complexity: O(N), where nn is the number of nodes in the linked list.
Space Complexity: O(N), primarily due to the space used by the hash map to store unique node values.
*/


Node *removeDuplicates(Node *head)
{
// Write your code here.
Node* currentNode = head;
Node* prevNode = NULL;
unordered_map<int, bool> visitedNode;

while(currentNode != NULL) {
if (!visitedNode[currentNode->data]) {
visitedNode[currentNode->data] = true;
prevNode = currentNode;
currentNode = currentNode->next;
}
else {
prevNode->next = currentNode->next;
free(currentNode);
}
currentNode = prevNode->next;
}
return head;
}