【Leetcode】82. Remove Duplicates from Sorted List II

Question:

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.

Tips:

跟83题比较,本题需要删除所有的重复数字,即只要一个数字出现重复,那么总第一个该数字开始都将被删除。

思路:

①设置一个新的头结点newHead,以及一个pre结点一个cur结点。将pre初始化为newHead,在pre之后找到第一个不重复的结点,并将其赋值给pre.next。

②递归。找到第一个不重复的结点,将它的next结点递归。

代码:

public ListNode deleteDuplicates1(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode newHead = new ListNode(-1);
        newHead.next = head;
        ListNode pre = newHead;
        ListNode cur = head;
        while (cur != null) {
            //找到第一个不重复的结点
            while (cur.next != null && cur.val == cur.next.val)
                cur = cur.next;
            //当pre的next就是cur即两者之间没有重复数字,将pre指针后移即可。
            if (pre.next == cur) {
                pre = cur;
            } else
                //否则 跳过cur 将pre的next设置成cur的next
                pre.next = cur.next;
            cur = cur.next;
        }
        return newHead.next;
    }

②:

public ListNode deleteDuplicates(ListNode head) {
        if (head == null)
            return null;

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

原文地址:https://www.cnblogs.com/yumiaomiao/p/8470239.html

时间: 2024-11-06 15:23:12

【Leetcode】82. Remove Duplicates from Sorted List II的相关文章

【一天一道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-&

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

【leetcode】26. 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 memory. 解题分析: 扫描一遍链表,用一个变量标记已找到的不重复的元

【LeetCode】26. 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 memory. For example,Given input array n

【LeetCode】83 - 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 memory. For example,Given input array nums 

【LeetCode】026. 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 memory. For example,Given input array n

【LeetCode】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. Solution: 1 /** 2 * Definition for singly-linked list. 3 * st

【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告

明日珠海行,心情紧张,写博文压压惊 囧 ------------------------------------- 原题地址: 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

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.