思路:
找到相等点的上一个点,然后netx指针更新一下。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val == val) return head->next;
for(auto p = head; p; p = p->next){
if(p->next && p->next->val == val){
auto tmp = p->next;
p->next = p->next->next;
}
}
return head;
}
};
评论区