83. Remove Duplicates from Sorted List

Input: head = [1,1,2]
Output: [1,2]
Input: head = [1,1,2,3,3]
Output: [1,2,3]
/**
* 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* deleteDuplicates(ListNode* head) {

int vn[101] = {0};

int vp[101] = {0};

ListNode *prev = head;

ListNode *curr = head;

while(curr!=NULL){

if(curr->val > 0 && vp[(curr->val)] == 1){
prev->next = curr->next;
curr = curr->next;
continue;
}

if(curr->val <= 0 && vn[-(curr->val)] == 1){
prev->next = curr->next;
curr = curr->next;
continue;
}

if(curr->val <= 0 ){
vn[-(curr->val)] = 1;
prev = curr;
curr = curr->next;
}
else if(curr->val > 0 ){
vp[(curr->val)] = 1;
prev = curr;
curr = curr->next;
}
}

return head;
}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store