美文网首页
82. Remove Duplicates from Sorte

82. Remove Duplicates from Sorte

作者: jecyhw | 来源:发表于2019-06-04 09:52 被阅读0次

题目链接

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

代码

class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (head == NULL || head->next == NULL) {
            return head;
        }

        ListNode *pre = NULL, *cur = head, *next = cur->next;
        while (next != NULL) {
            if (cur->val == next->val) {
                int val = cur->val;
                cur = next->next;
                while (cur != NULL && cur->val == val) {
                    cur = cur->next;
                }
                //是不是链表的前两个元素相等
                if (pre == NULL) {
                    head = cur;
                } else {
                    pre->next = cur;
                }
            } else {
                //往后移
                pre = cur;
                cur = pre->next;
            }
            if (cur != NULL) {
                next = cur->next;
            } else {
                break;
            }
        }
        return head;
    }
};

相关文章

网友评论

      本文标题:82. Remove Duplicates from Sorte

      本文链接:https://www.haomeiwen.com/subject/mizkxctx.html