【leetcode】Remove Duplicates from Sorted List II (middle)

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.

思路:

常规思路,关键点:用伪头部避免新建链表头的麻烦。

ListNode *deleteDuplicates2(ListNode *head) {
        if(head == NULL || head->next == NULL) return head;
        ListNode fakehead(0); //伪头结点  避免头结点建立的好方法
        ListNode * newtail = &fakehead;
        ListNode * p = head;
        int preval = head->val + 1; //记录前面结点的值 初始化为一个跟头结点值不同的数

        while(p != NULL)
        {
            if(p->val == preval) //数字出现过
            {
                p = p->next;
            }
            else //新数字
            {
                preval = p->val;
                if(p->next != NULL && p->next->val == preval) //新数字的下一个数字还是这个数 跳过
                 {
                    p = p->next->next;
                }
                else  //如果确定是一个独立的数字
                {
                        newtail->next = p;
                        p = p->next;
                        newtail = newtail->next;
                        newtail->next = NULL;
                }
            }
        }
        return fakehead.next;
    }

大神不需要伪头部的方法,利用了指针的地址。直接更改地址里面放的指针。

 class Solution {
public:
    ListNode *deleteDuplicates(ListNode *head) {
        ListNode** p = &head;
        while(*p && (*p)->next) {
            ListNode* p1 = *p, *p2 = p1->next;
            while(p2 && p2->val == p1->val) { //直接循环到下个数字出现的位置
                p2 = p2->next;
            }
            if(p2 != p1->next) {
                *p = p2;
            } else {
                p = &(p1->next);
            }
        }
        return head;
    }
};
时间: 2024-08-14 21:10:33

【leetcode】Remove Duplicates from Sorted List II (middle)的相关文章

【leetcode】Search in Rotated Sorted Array II(middle)☆

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

【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->3.

【Leetcode】Remove Duplicates from Sorted Array II

题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出现超过两次了:否则满足要求. 注意:通过上面的思路可知,特判情况是长度小于等于2时. PS:提交后16ms,在此题的提交时间第一队列最前位置.Yes! 代码: class Solution { public: int removeDuplicates(vector<int>& nums)

【LeetCode】Remove Duplicates from Sorted Array 解题报告

[LeetCode]Remove Duplicates from Sorted Array 解题报告 标签(空格分隔): LeetCode [LeetCode] https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Total Accepted: 129010 Total Submissions: 384622 Difficulty: Easy Question Given a sorted array, remov

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(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

80. Remove Duplicates from Sorted Array II(js)

80. Remove Duplicates from Sorted Array II Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying t

【leetcode】Remove Duplicates from sorted array

Remove Duplicates from sorted array 问题描述 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant me

LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)

Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't