Leetcode 203 Remove Linked List Elements 链表
去掉链表中相应的元素值
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* removeElements(ListNode* head, int val) { 12 if(!head) return NULL; 13 ListNode* now = head; 14 for(;now->next ;){ 15 if(now->next->val == val){ 16 ListNode* next = now->next; 17 now->next = next->next; 18 delete next; 19 } 20 else now = now->next; 21 } 22 if(head->val == val) { 23 ListNode* t = head; 24 head = head->next; 25 delete t; 26 } 27 return head; 28 } 29 };