Problem:
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
思路:
Solution (C++):
ListNode* removeElements(ListNode* head, int val) {
ListNode dummyNode(0);
using L = ListNode*;
L dummy = &dummyNode, pre = dummy, cur = head;
dummy->next = head;
while (cur) {
while (cur && cur->val == val) {
cur = cur->next;
pre->next = cur;
}
if (cur) {
pre = cur;
cur = cur->next;
}
}
return dummy->next;
}
性能:
Runtime: 44 ms Memory Usage: 10.8 MB
思路:
Solution (C++):
性能:
Runtime: ms Memory Usage: MB