TimeOut and Run time error ??? but its correct... Please help me in finding mistake..


  • 0
    M
    class Solution {
    public:
        ListNode *deleteDuplicates(ListNode *head) {
            ListNode *temp, *next, *del;
            temp = head;
            
            if(temp == NULL)
                return NULL;
            
            while(temp->next!=NULL)
            {
                if(temp->next->val == temp->val)
                {
                    del = temp->next;
                    temp->next = temp->next->next;
                    delete del;
                }
                temp = temp->next;
            }
            return head;
        }
    };

  • 2
    L
    1. did the question said the input is a *sorted" linked list? If not, your solution doesn't work. You can save the value in a hashtable.
    2. Assuming the input is *sorted", what if the input is 1->1->1 ? your output is 1->1, still has duplicates

  • 0
    M

    Thank you littlestone a lot !!. I highly appreciate your kind efforts...


  • 2
    S

    Hi, I think you made the same mistake as me. You should use {else} here because otherwise your temp = temp->next->next when duplicates occur. And for input {1,1}, your temp goes out of boundary already and while loop will throw exception. Hope it helps

            if(temp->next->val == temp->val)
            {
                del = temp->next;
                temp->next = temp->next->next;
                delete del;
            }
            else{
                 temp = temp->next;
            }

  • 0
    S

    While the problem does not specify a sorted list, the examples only show removing consecutive duplicates, and only removing consecutive duplicates is sufficient for acceptance. A successor problem requiring {1,2,1,3} => {1,2,3} might be interesting, but this problem does not require that.


  • 0
    S

    Timeout is not an error in your code, it means the server was too busy and gave up trying to judge your submission - you should wait at least 10 seconds and try again. If your code took too long the message would be "Time Limit Exceeded". See the FAQ: https://oj.leetcode.com/faq/


Log in to reply
 

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