Untitled
unknown
plain_text
a year ago
1.0 kB
13
Indexable
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* modifiedList(vector<int>& nums, ListNode* head) {
unordered_map < int , bool > seen;
for(int i=0;i<nums.size();i++) {
seen[nums[i]] = true;
}
ListNode *prev = NULL, *current = head;
while(current!=NULL) {
if(seen[current->val]) {
if(head == current) {
prev = head;
head = current->next;
}
else {
prev->next = current->next;
}
} else {
prev = current;
}
current = current->next;
}
return head;
}
};Editor is loading...
Leave a Comment