최대 1 분 소요

[leetcode] Remove Linked List Elements

문제 링크

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

카테고리:

업데이트: