Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Here are all my solutions for the GFG POTD..... Back to upload guyz....

NotificationsYou must be signed in to change notification settings

getlost01/gfg-potd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Today - 3 July 2024

Que - Remove all occurences of duplicates in a linked list

The problem can be found at the following link:Question Link

My Approach

  • Create an unordered map mp to count the occurrences of each value in the linked list.
  • Traverse the linked list using a pointer temp and update the map with the count of each node's data.
  • Create a dummy node that points to the head of the linked list. This helps in handling edge cases where the head node might need to be removed.
  • Initialize a pointer prev to the dummy node.
  • Reset temp to the head of the linked list.
  • Traverse the linked list again using temp.
  • For each node, check the count of its data in the map:
  • If the count is greater than 1, it means the node is a duplicate, so update the next pointer of prev to skip the current node (temp).
  • If the count is 1, move the prev pointer to the current node (temp).
  • Move the temp pointer to the next node.
  • Set new_head to the next of the dummy node.
  • Delete the dummy node to free up memory.
  • Return new_head, which is the head of the modified linked list with duplicates removed.

Time and Auxiliary Space Complexity

  • Time Complexity:O(N), whereN is the number of nodes in the linked list.
  • Auxiliary Space Complexity:O(N), whereN is the number of nodes in the linked list.

Code (C++)

classSolution {public:    Node*removeAllDuplicates(structNode* head)    {        unordered_map<int,int>mp;        Node* temp=head;while (temp)        {            mp[temp->data]++;            temp=temp->next;        }        Node* dummy=newNode(0);        dummy->next=head;        Node* prev=dummy;        temp=head;while (temp)        {if (mp[temp->data]>1)                prev->next=temp->next;else prev=temp;            temp=temp->next;        }        Node* new_head=dummy->next;delete dummy;return new_head;    }};

Contribution and Support

I always encourage contributors to participate in the discussion forum for this repository.

If you have a better solution or any queries / discussions related to theProblem of the Day solution, please visit ourdiscussion section. We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a⭐ star to thegetlost01/gfg-potd repository.

Total number of repository visitors


[8]ページ先頭

©2009-2025 Movatter.jp