Skip to content

Instantly share code, notes, and snippets.

@manojnaidu619
Created July 1, 2019 13:43
Show Gist options
  • Save manojnaidu619/d0fc8c1d60b8995f59ee91aa73b548af to your computer and use it in GitHub Desktop.
Save manojnaidu619/d0fc8c1d60b8995f59ee91aa73b548af to your computer and use it in GitHub Desktop.
Leetcode Solution for "Remove Linked List Elements" problem in CPP
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