Remove all elements from a linked list of integers that have value val.
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
/**
* 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) {
while(head && head->val ==val) head=head->next;
if(!head ) return NULL;
if( !head->next) return head;
ListNode* LN=head;
while(LN && LN->next)
{
if(LN->next->val ==val)
LN->next =LN->next->next;
else
LN=LN->next;
}
return head;
}
};
执行用时 :44 ms, 在所有 C++ 提交中击败了60.40%的用户
内存消耗 :10.8MB, 在所有 C++ 提交中击败了92.82%的用户