Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeElements(struct ListNode* head, int val) { struct ListNode* pre, *p; pre = p = head; while ( p ) { if ( p->val == val) { if ( p == head) { pre = head = p->next; } else { pre->next = p->next; } free(p); p = p->next; } else { pre = p; p = p->next; } } return head; }