leetcode 203. Remove Linked List Elements
程序员文章站
2024-02-19 08:27:34
...
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 Accepted
增加头节点,以防止溢出
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
ListNode *pseudo_head =new ListNode(0),*p;
pseudo_head->next=head;
p=pseudo_head;
while(p){
if(p->next && p->next->val==val) p->next=p->next->next;
//防止溢出的关键是先判断那个指针存在,再去用它的值
else p=p->next;
}
return pseudo_head->next;
}
};
推荐阅读
-
leetcode 203. Remove Linked List Elements
-
203. Remove Linked List Elements
-
Leetcode 203. Remove Linked List Elements
-
203. Remove Linked List Elements
-
【leetcode】203. Remove Linked List Elements
-
LeetCode笔记:237. Delete Node in a Linked List
-
LeetCode 237. Delete Node in a Linked List
-
Leetcode--Reverse Linked List
-
【LeetCode OJ 328】Odd Even Linked List
-
leetcode 328. Odd Even Linked List