标签:color amp null else bsp tno hat turn rem
203. Remove Linked List Elements
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
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 ListNode *pre = new ListNode(-1), *cur = pre; 13 pre->next = head; 14 while (cur->next != nullptr) { 15 if (cur->next->val == val) { 16 ListNode *t = cur->next; 17 cur->next = cur->next->next; 18 t->next = nullptr; 19 delete t; 20 } else { 21 cur = cur->next; 22 } 23 } 24 return pre->next; 25 } 26 };
203. Remove Linked List Elements 删除链表中val
标签:color amp null else bsp tno hat turn rem
原文地址:http://www.cnblogs.com/xumh/p/7725352.html