leetcode #82 in cpp

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Solution: 

The solution is to use a pointer 'cur' pointing to a node. Then we find out if cur node has duplicates, by comparing its next val to its val. If its next is a duplicate, mark cur as 'should be deleted', and then delete its next. We use this method to delete all duplicates of cur node. After deleting all cur's duplicates,  if cur is marked as 'should be deleted', then we delete cur node. 

Example: [1,1,1,2,2,3,4,5,5, 6], shoudDelete = false; cur node is the first node. 

1. 1(cur)->1(next)->1->2->2->3->4->5->5->6, next is the same, delete next. Mark shouldDelete.

2. 1(cur)-> 1(next)-> 2->2->3->4->5->5->6. next is the same, delete next. 

3.1(cur)->2(next)->2->3->4->5->5->6. next is not the same. Up to now we delete all duplicates of 1. 

4. Since cur is marked as shouldDelete, 1 is delete. cur goes to 2. 

5. 2(cur)->2(next)->3->4->5->5->6. next is the same, mark shouldDelete. And delete next. 

6. 2(cur)->3(next)->4->5->5->6. Because of shouldDelete, 2 is deleted. 

7. 3(cur)->4(next)->5->5->6. 3 is not the same as next, move cur. 

8. 3->4(cur)->5(next)->5(next)->6. 4 is not the same as next, move cur. 

9.3->4->5(cur)->5(next)->6. 5 is the same as next. Mark shouldDelete and delete next.

10. 3->4->5(cur)->6(next). delete cur. 

11. 3->4->6(cur)->null. 

Done.

Code:

/**
 * 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 || !(head->next)) return head;
        
        ListNode *cur = head;
        ListNode *prev = head;
        bool deleteCur = false;
        
        while(cur){
            
            deleteCur = false;
            
            while(cur->next&&cur->next->val == cur->val){//if current node is duplicated, delete its duplicates after it
                deleteCur = true;
                cur->next = cur->next->next;
            }
            
            if(deleteCur){//if current node is duplicated, we need to delete current node
                if(cur==head){//if current node is the head, update the head
                    head = head->next;
                    prev = head;
                    cur = head;
                }else{//if current node is not the head, simply put its prev's next = current node's next, and update cur
                    prev->next = cur->next;
                    cur = prev->next;
                }
            }else{//if current node is not duplicated, move prev to current number, and move cur to next number
                prev = cur;
                cur = cur->next;
            }
            
        }
        return head;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值