Leetcode[82]-Remove Duplicates from Sorted List II

Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

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, return1->2->5.
Given 1->1->1->2->3, return 2->3.


思路:跟上道题Leetcode[83]-Remove Duplicates from Sorted List算法有点区别,这道题需要设置一个标示符,如果某一趟比较的时候两个元素相等了,就设flag等于true,接着下一趟循环如果两个元素不相等,但此时的flag为true,就需要将两个元素前面的那个删掉。

最后,必定第二个元素为空而终止,此时还需要判断flag是否为true,如果为true,则末尾的元素还需要删掉;

代码如下:
Code(c++):

/**
 * 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) {
       ListNode * newList = new ListNode(-1);
        newList->next = head;
        ListNode *pre = newList;

        bool flag = false;
        while(pre->next && pre->next->next){
            if(pre->next->val == pre->next->next->val){
                pre->next = pre->next->next;
                flag = true;
                continue;
            }
            if(flag == true){
                pre->next = pre->next->next;
                flag = false;
                continue;
            }
            pre = pre->next;
        }
        //最后还需要判断是否上次判断时元素重复
        if(flag == true){
            pre->next = pre->next->next;
        }
        head = newList->next;
        return head;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值