C++ simple and easy-understanding


  • 0
    X
    ListNode* removeElements(ListNode* head, int val) {
            ListNode* tempHead = new ListNode(-1);
            tempHead->next = head;
            for(ListNode* i = tempHead;i != NULL && i ->next != NULL;){
                if(i->next->val == val){
                    ListNode* tmp = i->next;
                    i->next = i->next->next;
                    delete tmp;
                }else{
                    i = i->next;
                }
            }
            ListNode* ans = tempHead->next;
            delete tempHead;
            return ans;
        }

Log in to reply
 

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