Can we only delete the Nodes one by one?


  • 0
    A
    class Solution {
    public:
        ListNode* deleteDuplicates(ListNode* head) {
            
            if(head==nullptr)
                return head;
                
            ListNode* cur=head;
            ListNode* index=cur;
            
            bool flag;
            while(cur!=nullptr)
            {
                flag=false;
                index=cur->next;
                while(index!=nullptr && index->val==cur->val ){
                    index=index->next;
                    flag=true;
                }
    
                if(flag){ 
                if(index!=nullptr){    
                cur->val=index->val;
                cur->next=index->next;}
                else
                {
                    cur->val=0;
                }
                }
                else{
                cur->next=index;
                
                cur=index;}
            }
            
            return head;
            
        }
    };

  • 0

    This is just too.. straightforward ^^


Log in to reply
 

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