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.

这道题目是前一个题目的进化版,由于重复了的那个数字会被全部删去,这里面临一个多引入一个指针,来表征需要删除的节点的前一个节点,再多引入一个布尔值,

来标记到底这个数字是不是重复了;如果重复,位于pos 以及 next_pos之间的节点必须都删除(闭区间),pre_pos 很显然原地不动,当发现不是重复,那么pre_pos

向前走一格。

一个注意点:在思考链表类问题的时候,必须很明确每个指针标志着什么,并且需要再用到诸如a->next  a->val时特别确信a != NULL, 这个必须考虑到边界条件,如

何时走到了最后,或者链表只有一个节点或者没有节点,出现了特殊情况等等,这个是RUNTIME ERROR的关键问题

<span style="font-family: Arial, Helvetica, sans-serif;">ListNode *deleteDuplicates(ListNode *head) {</span>
        if (head == NULL || head->next == NULL)
            return head;

        bool isDup;
        ListNode *pos = head, *next_pos = head;

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

        while (next_pos != NULL)
        {
            next_pos = next_pos->next;
            isDup = false;
            while (next_pos != NULL && pos->val == next_pos->val)
            {
                isDup = true;
                next_pos = next_pos->next;
            }
            if (isDup)
            {
                pre_pos->next = next_pos;
                pos = next_pos;
            }
            else
            {
                pre_pos = pre_pos->next;
                pos = pos->next;
            }
        }
        return new_head->next;
    }
};

Remove Duplicates from Sorted List II [详细分析]

时间: 2024-10-07 06:39:03

Remove Duplicates from Sorted List II [详细分析]的相关文章

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刷题笔记】Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 题解: 设置两个变量:右边kepler和前向游标forward.如果当前kepeler所指的元素和

LintCode - Remove Duplicates from Sorted List II Show result

LintCode - Remove Duplicates from Sorted List II Show result LintCode - Remove Duplicates from Sorted List II Show result Web Link Description Code - C Tips Web Link http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/ Descriptio

[leetcode笔记] 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-&

[leetcode]Remove Duplicates from Sorted List II @ Python

原题地址:https://oj.leetcode.com/problems/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-&g

Leetcode 线性表 Remove Duplicates from Sorted Array II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Duplicates from Sorted Array II Total Accepted: 10649 Total Submissions: 35325 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorte

80. Remove Duplicates from Sorted Array II

/* * 80. Remove Duplicates from Sorted Array II * 2016-5-13 by Mingyang * 这里用了通式,就是k的值可以根据需要随时改变,注意这里++j的用法,最后return ++j * 注意的是题目不光要返回int,还要把array给换了 */ public int removeDuplicates2(int[] A,int k){ int len=A.length; if(len<k) return len; int j=0; int

[LeetCode][JavaScript]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 --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2