32ms C++ AC solution


  • 10
    B
    /**
     * 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 (head==NULL){
                return head;
            }
            ListNode* p=head;
            while(p->next!=NULL){
                if (p->next->val == val){
                    p->next = p->next->next;
                }
                else{
                    p=p->next;
                }
            }
            if (head->val==val)
                head=head->next;
            return head;
        }
    };

  • 0
    Q

    very nice, better than the ListNode ** version, especially when the data member val is not trivial as an int.


  • 1
    D

    Don't we need to free the removed ListNodes?


  • 0
    B

    yes, we do. I forget about that. I should do it after "p->next = p->next->next;"


  • 2
    B
    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            if (head==NULL){
                return head;
            }
            ListNode* p=head;
            while(p->next!=NULL){
                if (p->next->val == val){
                    auto freeNode = p->next;
                    p->next = p->next->next;
                    free(freeNode);
                }
                else{
                    p=p->next;
                }
            }
            if (head->val==val)
                head=head->next;
            return head;
        }
    };

  • 0
    T

    Hi, nice code! I just want to know what does AC mean? Is it some sort of algorithm name?
    Thanks


  • 0
    H

    It means accept.


  • 0
    C

    @TarzanNJane
    Abbreviation of accept


  • 0
    H

    share my solution similar to yours;

    ListNode* removeElements(ListNode* head, int val) {
             while(head && head->val == val) head = head->next; 
             if(head == nullptr)    return nullptr;
             
             ListNode *p = head->next;
             ListNode *prev = head;
             while(p) {
                 if(p->val == val) {
                     prev->next = p->next;
                     p = p->next;
                 } else {
                     prev = p;
                     p = p->next;
                 }
             }
             return head;
        }
    

  • 0
    D

    @benyin You're forgetting a free on your very last conditional before you return. In the case where the head has the val you want to skip over it but also free the old head right?


  • 0
    M

    My code:

    class Solution {
    public:
        ListNode* removeElements(ListNode* head, int val) {
            auto vir = new ListNode(0),
                 p = vir;
            vir->next = head;
            
            while (p->next != NULL) {
                auto t = p->next;
                if (t->val == val) {
                    p->next = t->next;
                    delete t;
                } else {
                    p = p->next;
                }
            }
            return vir->next;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.