首页 > 编程语言 > 详细

leetcode 82 删除排序列表中重复元素

时间:2021-02-15 23:21:47      阅读:24      评论:0      收藏:0      [点我收藏+]

没啥太难的思路,代码如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution 
{
public:
    ListNode* deleteDuplicates(ListNode* head) 
    {
        ListNode* temp1 = new ListNode(0,head);
        ListNode* temp = temp1;
        ListNode* temp2 = head;
        if(!head || !head->next)
        return head;
        while(temp2 && temp2->next)
        {
            if(temp2->val == temp2->next->val)
            {
                while(temp2->next && temp2->val == temp2->next->val)
                temp2 = temp2->next;
                temp2 = temp2->next;
                temp1->next = temp2;
            }
            else
            {
                temp1 = temp2;
                temp2 = temp2->next;
            }          
        }
        return temp->next;
    }
};

 

leetcode 82 删除排序列表中重复元素

原文:https://www.cnblogs.com/zhaohhhh/p/14403716.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!