llllmz

导航

203. 移除链表元素

老题新做

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        if(!head) return nullptr;
        if(head->val != val){
            head->next = removeElements(head->next, val);
        }else{
            ListNode* temp = head->next;
            delete head;
            return removeElements(temp, val);
        }
        return head;
    }
};

 

posted on 2024-09-20 22:18  神奇的萝卜丝  阅读(12)  评论(0)    收藏  举报