My code for problem 82


  • 0
    X

    Hello,

    My code cannot pass test like: 1->1 or 1->1->1->2->2

    Still cannot figure out the reason, please advice!

    class Solution {
     public:
      ListNode* deleteDuplicates(ListNode* head) {
        if(head == NULL || head->next==NULL) return head;
        ListNode* temp = new ListNode(0);
        temp->next = head;
        ListNode* cur = temp;
        ListNode* ite = head;
        int dup = 0;
    
        while(ite->next != NULL){
    
          while(ite->next->val == ite->val){
            ite=ite->next;
            dup=1;
            cout<<ite->val<<endl;
          }
          if(dup){
            dup=0;
            ite=ite->next;
            cout<<ite->next<<endl;
            cout<<ite->val<<endl;
          }else{
            cur->next = ite;
            cur = cur->next;
            ite=ite->next;
          }
    
        }
        cur->next = ite;
        return temp->next;
      }
    };

Log in to reply
 

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