203. Remove Linked List Elements
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
相关链接如下:
知乎:littledy
GitHub主页:https://github.com/littledy
github.io:https://littledy.github.io/
欢迎关注个人微信公众号:小邓杂谈,扫描下方二维码即可
![](https://www.cnblogs.com/images/cnblogs_com/dysjtu1995/1468066/t_qrcode_for_gh_547d3edeac81_258.jpg)
作者:littledy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。