移除链表元素
删除链表中等于给定值 val 的所有节点。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(head == NULL){ return head; } while(head != NULL and head->val == val){ head = head->next; } if(head == NULL || head->next == NULL){ return head; } else{ ListNode *s = head; ListNode *f = s->next; while(f != NULL){ if(f->val == val){ s->next = f->next; f = s->next; continue; } s = s->next; f = f->next; } return head; } } };