码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】Remove Duplicates from Sorted List II

时间:2014-12-06 16:48:45      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   sp   for   strong   

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

 

把非重复元素放置到新的链表中。

新链表设置表头newhead,以免纠结重复的元素是否为头结点的边界情况,反正返回的是newhead->next。

cur指针在原链表中遍历,如果与新链表尾newtail不同,则新链表新增元素;若相同,把newtail一并删去。

代码简单不多解释了。

/**
 * 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==NULL || head->next==NULL)
            return head;
        else
        {//at least two nodes
            ListNode* cur = head->next;
            ListNode* newhead = new ListNode(-1);
            ListNode* newtail = new ListNode(head->val);
            newhead->next = newtail;
            ListNode* pretail = newhead;
            while(cur != NULL)
            {
                if(cur->val != newtail->val)
                {
                    newtail->next = new ListNode(cur->val);
                    cur = cur->next;
                    pretail = newtail;
                    newtail = newtail->next;
                }
                else
                {
                    while(cur != NULL && cur->val == newtail->val)
                        cur = cur->next;
                    //cur == NULL or cur diffs newtail
                    if(cur == NULL)
                    {
                        pretail->next = NULL;   //delete newtail
                        return newhead->next;
                    }
                    else
                    {
                        newtail = new ListNode(cur->val);
                        pretail->next = newtail;
                        cur = cur->next;
                    }
                }
            }
            return newhead->next;
        }
    }
};

bubuko.com,布布扣

 

有人指出我的算法创建了太多新节点,空间复杂度较高。

其实很好改,把原链表上的节点“拆”下来装到新链表尾部就行了。

/**
 * 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==NULL || head->next==NULL)
            return head;
        else
        {//at least two nodes
            ListNode* cur = head->next;
            ListNode* newhead = new ListNode(-1);
            ListNode* newtail = new ListNode(head->val);
            newhead->next = newtail;
            ListNode* pretail = newhead;
            while(cur != NULL)
            {
                if(cur->val != newtail->val)
                {
                    newtail->next = cur;
                    cur = cur->next;
                    pretail = newtail;
                    newtail = newtail->next;
                    newtail->next = NULL;   //cut off
                }
                else
                {
                    while(cur != NULL && cur->val == newtail->val)
                        cur = cur->next;
                    //cur == NULL or cur diffs newtail
                    if(cur == NULL)
                    {
                        pretail->next = NULL;   //delete newtail
                        return newhead->next;
                    }
                    else
                    {
                        newtail = cur;
                        pretail->next = newtail;
                        cur = cur->next;
                        newtail->next = NULL;   //cut off
                    }
                }
            }
            return newhead->next;
        }
    }
};

bubuko.com,布布扣

【LeetCode】Remove Duplicates from Sorted List II

标签:des   style   blog   http   io   color   sp   for   strong   

原文地址:http://www.cnblogs.com/ganganloveu/p/4148401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!