首页 > 代码库 > 【LeetCode】Remove Duplicates from Sorted List II

【LeetCode】Remove Duplicates from Sorted List II

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

 

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

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

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

【LeetCode】Remove Duplicates from Sorted List II