Why "Time Limit Exceeded" for this code?


  • 0
    C
    ListNode *deleteDuplicates(ListNode *head) {
            ListNode* cur=head;
            while (cur){
                while(cur->next ){
                    if (cur->val==cur->next->val)
                        cur->next=cur->next->next;
                }
                cur=cur->next;
            }
            return head;
        }
    

    Is the code above equivalent to the accepted answer below?

    ListNode *deleteDuplicates(ListNode *head) {
            ListNode* cur=head;
            while (cur){
                while(cur->next && cur->val==cur->next->val){
                    //if (cur->val==cur->next->val){
                        cur->next=cur->next->next;
                    //}
                }
                cur=cur->next;
            }
            return head;
        }
    

    Any feedback is greatly appreciated!


  • 0
    J

    In your code, if cur->vale != cur->next->val, cur->next would be never changed.


Log in to reply
 

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