LeetCode 82 - Remove Duplicates from Sorted List II

原创 2015年09月07日 12:30:15

Remove Duplicates from Sorted List II

题目链接

https://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->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

解题思路

一. 递归法
递归跳出条件:当前结点或当前结点的下一结点为空
递归过程:若当前结点值与下一结点值相同,则找到第一个不相同的结点,返回对该结点的递归结果;若当前结点值与下一结点值不同,则令当前结点的next域指向下一结点的递归结果,并返回当前结点指针

二. 迭代法(常规法)
1. 创建一个新的头结点L作为返回,设置工作指针*p及判别指针*p1、*p2,初始时令*p指向L,*p1指向所给链表的第一个结点
2. 扫描链表,令*p2指向与*p1值不相同的第一个结点,若p2不是p1的next指向,则将p1挂在L之后
3. 令p1指向p2,重复步骤2
4. 扫描结束,返回L的next域

Tips:
1. 使用迭代法时可设置挂载标记,以减少对空指针的判定
2. 迭代法循环结束后,注意将最后一个结点加入结果链表

代码实现(C++)

递归法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head || ! head -> next) return head;

        ListNode* p = head -> next;
        if (p->val == head->val) {
            while (p && p->val == head->val) {
                p = p->next;    // 寻找第一个不相同的结点
            }
            return deleteDuplicates(p);
        } else {
            head->next = deleteDuplicates(p);
            return head;
        }
    }
};

迭代法

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if (!head || ! head -> next) return head;

        ListNode* L = new ListNode(-1);    // 结果链表头结点
        ListNode *p, *p1, *p2;
        bool addFlag;    // 挂载标记

        p = L; p1 = head;
        while (p1 && p1->next) {
            addFlag = true;
            p2 = p1->next;

            while (p2 && p2->val == p1->val) {
                p2 = p2->next;
                if (addFlag) addFlag = false;
            }

            if (addFlag) {    // 满足挂载条件,将结点加入结果链表
                p->next = p1;
                p = p->next;
            }

            p1 = p2;
        }

        p->next = p1;    // 将最后一个结点接入链表
        return L->next;
    }
};

效率

两种方法均只对链表遍历一遍,时间复杂度:O(n)
两种方法均只需固定空间开销,空间复杂度:O(1)

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

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/reve...
  • u012975705
  • u012975705
  • 2015-12-27 00:18:19
  • 1401

LeetCode 82之Remove Duplicates from Sorted List II的Java题解

题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dist...
  • u012249528
  • u012249528
  • 2015-05-06 17:23:34
  • 451

82. Remove Duplicates from Sorted List II Leetcode Python

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb...
  • hyperbolechi
  • hyperbolechi
  • 2015-02-05 11:48:11
  • 1013

leetcode || 82、Remove Duplicates from Sorted List II

problem: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onl...
  • hustyangju
  • hustyangju
  • 2015-04-13 16:47:33
  • 839

LeetCode(83)Remove Duplicates from Sorted List

题目如下: Given a sorted linked list, delete all duplicates such that each element appear only once. For...
  • feliciafay
  • feliciafay
  • 2014-01-10 08:06:49
  • 2870

Remove Duplicates from Sorted List II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/  这道题跟Remove Duplicates...
  • linhuanmars
  • linhuanmars
  • 2014-04-24 08:33:10
  • 10838

Remove Duplicates from Sorted List -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/  这是一道比较简单的链表操作的题目,要求是删去有序...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 11:25:06
  • 8932

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplica...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39:29
  • 11025

LeetCode 83 — Remove Duplicates from Sorted List(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, ...
  • dragon_dream
  • dragon_dream
  • 2014-04-01 19:43:21
  • 2536

【LeetCode with Python】 Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb...
  • nerv3x3
  • nerv3x3
  • 2014-08-29 18:36:24
  • 2762
收藏助手
不良信息举报
您举报文章:LeetCode 82 - Remove Duplicates from Sorted List II
举报原因:
原因补充:

(最多只允许输入30个字)