Accepted code with weak point (C++) (suggestion for new test)


  • 0
    N

    I have a simple question. My version of solution worked pretty well. But I think that there should be test for long list.

    For example, my version based on recursion and it wouldn't work on long list. But it was accepted.

    ListNode* removeElements(ListNode* head, int val) {
    	if (head == NULL)
    		return head;
    	if (head->next == NULL && head->val == val)
    	{
    		delete head;
    		return NULL;
    	}
    	if (head->val == val)
    	{
    		ListNode *next = head->next;
    		head->val = next->val;
    		head->next = next->next;
    		delete next;
    		return removeElements(head, val);
    	}
    
    	if (removeElements(head->next, val) == NULL)
    		head->next = NULL;
    	return head;
    }
    

    So, question is: Why not add new test with long list?


Log in to reply
 

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