82. Remove Duplicates from Sorted List II && i

题目

83. Remove Duplicates from Sorted List

 Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3. 

解析

class Solution_83 {
public:
    ListNode *deleteDuplicates(ListNode *head) {

        if (!head||!head->next)
        {
            return head;
        }

        ListNode* cur = head;
        ListNode*pre = NULL;
        while (cur&&cur->next)
        {
            pre = cur;
            cur = cur->next;
            ListNode* temp = pre; //记录每次重复点的开始位置
            while(cur&&pre->val==cur->val)
            {
                pre = cur;
                cur=cur->next;
            }
            temp->next = cur; //跳过重复位置
        }
        return head;
    }
};

82. 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. 

解析

  • 由于链表开头可能会有重复项,被删掉的话头指针会改变,而最终却还需要返回链表的头指针。所以需要定义一个新的节点,然后链上原链表,然后定义一个前驱指针和一个现指针,每当前驱指针指向新建的节点,现指针从下一个位置开始往下遍历,遇到相同的则继续往下,直到遇到不同项时,把前驱指针的next指向下面那个不同的元素。如果现指针遍历的第一个元素就不相同,则把前驱指针向下移一位。

//参考容易理解一些
ListNode *deleteDuplicates(ListNode *head) {
        if (!head || !head->next) return head;

        ListNode *start = new ListNode(0);
        start->next = head;
        ListNode *pre = start;
        while (pre->next) {
            ListNode *cur = pre->next;
            while (cur->next && cur->next->val == cur->val) cur = cur->next;
            if (cur != pre->next) pre->next = cur->next;
            else pre = pre->next;
        }
        return start->next;
    }

// 82. Remove Duplicates from Sorted List II
class Solution_82 {
public:
    ListNode* deleteDuplicates(ListNode* head) {

        if (!head||!head->next)
        {
            return head;
        }

        ListNode*newHead = new ListNode(0);
        newHead->next = head;

        ListNode* pre = newHead;
        ListNode* cur = head;

        while (cur&&cur->next)
        {
            ListNode* next = cur->next;

            if(next->val!=cur->val)
            {
                if (pre->next==cur) //pre->next当前元素开始,cur当前元素结束,cur->next另外不同的元素
                {
                    pre = cur;
                }
                else
                {
                    pre->next = cur->next;
                }
            }
            cur = cur->next;
        }
        if (pre->next!=cur) //这里是地址比较,若没有重复元素,则地址相同的
        {
            pre->next = cur->next;
        }
        return newHead->next;
    }
};

题目来源

原文地址:https://www.cnblogs.com/ranjiewen/p/8744883.html

时间: 2024-11-05 15:56:25

82. Remove Duplicates from Sorted List II && i的相关文章

82. Remove Duplicates from Sorted List II(js)

82. 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. Example 1: Input: 1->2->3->3->4->4->5 Output: 1->2->5 Example

LeetCode(82): 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->

leetcode:82. Remove Duplicates from Sorted List II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50411033 题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have

82. 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.

【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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-&

82. Remove Duplicates from Sorted List II (List)

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

LeetCode OJ 82. 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.

leetCode 82.Remove Duplicates from Sorted List II (删除排序链表的反复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

[leedcode 82] 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.