This is my CPP solution.
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head==NULL)
return head;
while(head->val == val){
head = head->next;
if(head==NULL)
return head;
}
ListNode* p = head;
ListNode* q = head->next;
while(q!=NULL){
if(q->val == val){
p->next = q->next;
q = p-> next;
}
else{
p = q;
q = q->next;
}
}
return head;
}
};