Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* removeElements(struct ListNode* head, int val) 9 {10 if(head==NULL)11 return NULL;12 13 if(head->next==NULL&&head->val==val)14 {15 return NULL;16 }17 18 if(head->next==NULL&&head->val!=val)19 {20 return head;21 }22 23 24 25 struct ListNode *p=head;26 27 while(p->val==val&&p->next!=NULL)28 {29 p=p->next;30 }31 32 if(p->val==val)33 {34 return NULL;35 }36 37 38 head=p;39 40 if(p->next==NULL)41 {42 return head;43 }44 45 while(p->next->next!=NULL)46 {47 if(p->next->val==val)48 {49 p->next=p->next->next;50 continue;51 }52 p=p->next;53 }54 55 if(p->next->val==val)56 {57 p->next=NULL;58 }59 60 return head;61 }