Untitled

mail@pastecode.io avatar
unknown
plain_text
9 days ago
1.0 kB
5
Indexable
Never
/**
 * 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;
    }
};
Leave a Comment