# My solution and explaination.

• class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *h;
ListNode *p = h; //point p and h point to head;

``````    while(head != NULL){
}
if(head == NULL){                               //if head == NULL, it means that all nodes after last step equal val.
h->next = NULL;                            //then h next point to the tail(i.e. NULL)
break;
}
else{                                                  //if head not NULL, means find one node which not equal val and there is still nodes`s value not equal val.
h->next = head;                           //h next point to the nodes which not equal val
h = h->next;                                 //move h to the node which not equal val. it will point to next step result.
}
}
else{                                                    //if nodes value not equal val, head and h should point to next node;
h = h->next;
}
}
return p->next;
}
``````

};

• My answer is similar to yours.

``````    ListNode curr = head;
ListNode last = null;
while(curr!=null){
if(curr.val==val){