C++ solution easy


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

  • 5
    S

    Shouldn't we consider the necessity to free the memory ?


Log in to reply
 

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