leetcode 203. Remove Linked List Elements
题目描述:
/** * 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 NULL; } ListNode *newHead = new ListNode(-1); newHead->next = head; ListNode *cur = head; ListNode *pre = newHead; while(cur!=NULL){ if(cur->val == val){ cur = cur->next; pre->next = cur; } else{ pre = cur; cur = cur->next; } } return newHead->next; } };
幸运之神的降临,往往只是因为你多看了一眼,多想了一下,多走了一步。