Created
July 1, 2019 13:43
-
-
Save manojnaidu619/d0fc8c1d60b8995f59ee91aa73b548af to your computer and use it in GitHub Desktop.
Leetcode Solution for "Remove Linked List Elements" problem in CPP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
ListNode* removeElements(ListNode* head, int val) { | |
struct ListNode *p = head, *q=NULL; | |
if(head==NULL){ | |
return head; | |
} | |
while(p->val == val && p->next!=NULL){ | |
p=p->next; | |
head = p; | |
} | |
if(p->val==val){ | |
return NULL; | |
} | |
while(p->next!=NULL){ | |
q=p; | |
p=p->next; | |
if(p->val == val){ | |
while(p->next != NULL && p->next->val == val){ | |
p=p->next; | |
} | |
q->next = p->next; | |
} | |
} | |
return head; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment