删除链表中某特定值的节点

简单的递归解法

zzas12345发布于 几秒前0C++

解题思路

考虑头部
考虑连续重复节点

代码

/**
 * 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(NULL == head) return NULL;
        if(head->val == val) return removeElements(head->next,val);
        ListNode * res = head;
        while(head&&head->next){
            while(head->next &&head->next->val==val){ head->next=head->next->next;}
            head=head->next;
        }
        return res;
    }
};
posted @ 2020-05-06 19:21  zzas12345  阅读(190)  评论(0)    收藏  举报