其他分享
首页 > 其他分享> > leetcode 82 删除排序列表中重复元素

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

作者:互联网

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

/**
 * 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;
    }
};

 

标签:head,ListNode,val,next,temp2,temp1,82,排序,leetcode
来源: https://www.cnblogs.com/zhaohhhh/p/14403716.html