Can't find errors in my code. Ask for your help.


  • 0
    L
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            if (head == NULL) return head;
            if(head->next == NULL) return head;
            ListNode* tmp = 0;
            ListNode* cur = head;
            ListNode* later = cur->next;
            while (later){
                if (cur->val != later->val){
                    tmp = cur;
                    cur = cur->next;
                    later = later->next;
                }else if(later->next){
                    while(later->next&&cur->val == later->val){
                        later = later->next;
                    }
                    cur = later;
                    tmp = cur;
                    later = cur->next;
                }else{
                    break;
                }
            }
            return tmp;
        }
    };

  • 0
    M

    for this test case[1,2,2]

    u will retrun pointer to node.val = 1 and tha't true but node.next will be = 2 coz u didn't free it.

    Then when auto judge check the output will see [1,2,2] not [1] as u want.


Log in to reply
 

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