题目:

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.

链接: http://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

题解:

链表去重II,这里要建立一个fake head,因为假如全部为重复,需要移除所有的元素。 还需要一个boolean变量来判断当前状态是否重复。最后判断循环结束时的边界状态。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode dummy = new ListNode(-1);
ListNode node = dummy;
boolean isDuplicate = false; while(head.next != null) {
if(head.next.val == head.val)
isDuplicate = true;
else {
if(!isDuplicate) {
node.next = head;
node = node.next;
} else
isDuplicate = false;
}
head = head.next;
} if(isDuplicate)
node.next = null;
else
node.next = head; return dummy.next;
}
}

二刷:

我发现自己的思路就是和自己的思路一样...磨蹭了半天,二刷还是写了跟一刷很类似的code....

我们主要就是用一个boolean hasDuplicate来记录之前是否出现过重复,以及一个dummy节点来保证假如链表头有重复我们也可以处理。

  1. 先做边界判断
  2. 建立fake head dummy, 以及 node = dummy
  3. 在head != null以及 head.next != null的条件下我们进行遍历
  4. 假如head.val == head.next.val, 我们判定hasDuplicate = true
  5. 否则head.val != head.next.val,这时候我们要进行分析
    1. 假如hasDuplicate =false,这时候我们这个head可以加入到结果之中去,我们执行node.next = head, node = node.next
    2. 否则我们不管
    3. 这时候重置hasDuplicate = false
  6. 每次head = head.next
  7. 最后判断最后一个元素,hasDuplicate为真时,我们把node.next设置为null,跳过最后一个重复元素, 否则node.next = head,返回结果

Java:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(-1);
ListNode node = dummy;
boolean hasDuplicate = false;
while (head != null && head.next != null) {
if (head.val == head.next.val) {
hasDuplicate = true;
} else {
if (!hasDuplicate) {
node.next = head;
node = node.next;
}
hasDuplicate = false;
}
head = head.next;
}
node.next = hasDuplicate ? null : head;
return dummy.next;
}
}

三刷:

思路跟上面都差不多

Java:

Time Complexity - O(n), Space Complexity - O(1)。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode node = dummy;
int count = 1;
while (head != null && head.next != null) {
if (head.val != head.next.val) {
if (count == 1) {
node.next = head;
node = node.next;
}
count = 1;
} else {
count++;
}
head = head.next;
}
node.next = (count == 1) ? head : null;
return dummy.next;
}
}

82. Remove Duplicates from Sorted List II的更多相关文章

  1. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  2. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  3. 82. Remove Duplicates from Sorted List II【Medium】

    82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...

  4. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  5. LeetCode OJ 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  6. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  7. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

  8. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...

  9. leetcode 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

随机推荐

  1. XHTML1.0对HTML4.0的改进

    1.XHTML借鉴了XML的写法,语法更加严格: 2.XHTML实现了把页面样式和内容分离了,废弃了HTML4.0中表示样式的标签和属性,推荐使用CSS样式来描述页面的样式. XHTML1.0 分为两 ...

  2. WPF 一个弧形手势提示动画

    这是一个操作提示动画,一个小手在屏幕上按照一个弧形来回运动 <Window x:Class="LZRichMediaWall.MainWindow" xmlns=" ...

  3. vim命令总结

    前言 本文翻译自:http://bencrowder.net/files/vim-fu/,参考了VIM中文帮助. Google翻译结果和实际操作结果,对原文的部分内容重新整理,删除和添加了 部分内容并 ...

  4. C#中DataTable与实体集合通用转换(使用扩展方法)

    本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...

  5. backup site collection

    http://stackoverflow.com/questions/5376380/sharepoint-2010-change-sitecollection-urlstsadm -o backup ...

  6. 【转】android 内存泄漏相关收藏博客。

    关于android内存泄漏的研究   博客建了几个月,都没有去写,一是因为当时换工作,然后又是新入职(你懂的,好好表现),比较忙:二是也因为自己没有写博客的习惯了.现在还算是比较稳定了,加上这个迭代基 ...

  7. windows下编译ffmpeg

    windows 编译ffmpeg 搞过很多次,每次总是磕磕碰碰,从头到尾不能一直顺利,浪费一些时间.终究起原因,都是当时记得,过段时间就忘了.好记性不如烂笔头,大好周末晚上,闲暇无事,记录最近一次编译 ...

  8. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

  9. 【BZOJ】【2594】【WC2006】水管局长数据加强版

    LCT 动态维护MST嘛……但是有删边= =好像没法搞的样子 离线记录所有修改&询问,倒序处理,就可以变删边为加边了- 论如何用LCT维护最小生成树:先搞出一棵最小生成树,然后每次加边(u,v ...

  10. Matlab找二维数组最大值

    1.m先max(x)求出每列最大值,返回行向量,再max对行向量求出最大值,就是max(max(x)). 注意:max(x),不管x是行列向量,只要是向量,那么就返回一个值. 2.先x(:)转为按列的 ...