[LeetCode-JAVA] 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指向新的位置,需要注意的是开始时候,需要进行额外的判断是否有重复,才能形成错位的指针。

原始代码:

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode req = head;
        ListNode pre = head;

        //判断初始是否有重复
        while(head != null){
            head = head.next;   // 直到没有重复,head位于pre的下一位
            if(head != null && pre.val == head.val){
                do{
                    head = head.next;
                }
                while(head!=null && head.val == pre.val);
                pre = head;
                req = head;
            }else break;
        }
        //过程中判断,pre始终在head前一位
        while(head != null){
            head = head.next;
            if(head!=null && pre.next.val == head.val){
                do{
                    head = head.next;
                }
                while(head!=null && head.val == pre.next.val);
                pre.next = head;
            }
            else pre = pre.next;
        }

        return req;
    }
}

修改思路:可以建立一个虚拟表头,next指向head,这样可以把原始代码中初始部分的额外判断优化掉,代码十分简洁。

修改后代码:

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dunmy = new ListNode(Integer.MIN_VALUE);
        dunmy.next = head;
        ListNode pre = dunmy;

        while(head != null){
            head = head.next;
            if(head!=null && pre.next.val == head.val){
                do{
                    head = head.next;
                }
                while(head!=null && head.val == pre.next.val);
                pre.next = head;
            }
            else pre = pre.next;
        }

        return dunmy.next;
    }
}
时间: 2024-11-13 06:57:21

[LeetCode-JAVA] Remove Duplicates from Sorted List II 优化版的相关文章

[LeetCode][Java] 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 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数大于两次,删除多余的复制,返回删除后数组长度,要求不另开内存空间. C++ 献上自己丑陋无比的代码.相当于自己实现一个带计数器的unique函数 class Solution { public: int removeDuplicates(std::vector<int>& nums) {

[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][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][JavaScript]Remove Duplicates from Sorted Array II

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 n

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] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C%2B%2B-Java-Python-Ruby 描述 Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1

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

[C++]LeetCode: 72 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]. 思路: 可以借鉴Remove Duplicates from Sorted Array