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.
链接: 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节点来保证假如链表头有重复我们也可以处理。
- 先做边界判断
- 建立fake head dummy, 以及 node = dummy
- 在head != null以及 head.next != null的条件下我们进行遍历
- 假如head.val == head.next.val, 我们判定hasDuplicate = true
- 否则head.val != head.next.val,这时候我们要进行分析
- 假如hasDuplicate =false,这时候我们这个head可以加入到结果之中去,我们执行node.next = head, node = node.next
- 否则我们不管
- 这时候重置hasDuplicate = false
- 每次head = head.next
- 最后判断最后一个元素,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的更多相关文章
- 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题要求 ...
- 82. Remove Duplicates from Sorted List II && i
题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...
- 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 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 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 ...
- [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 ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- [LeetCode#82]Remove Duplicates from Sorted Array II
Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...
- leetcode 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- Python 学习教程
<Core Python Programming>勘误参考表 http://starship.python.net/crew/wesc/cpp/errata2.htm 笨办法学 Pytho ...
- ccache高速编译工具
ccache的主页:http://ccache.samba.org distcc的主页:http://distcc.samba.org 1.背景: 在处理一些规模相对较大的工程时,编译花费的时间可能会 ...
- ios上取得设备唯一标志的解决方案
iOS 7中苹果再一次无情的封杀mac地址,现在已经不能获取ios7设备的物理地址.那么在开发中如何才能标识设备的唯一性呢?apple公司提供的方法是通过keychain来存一些标志信息,然后通过存的 ...
- 开发中/listfile.jsp(11,31) quote symbol expected 这个错误
可能是因为11行33列,少了一个引号.
- iOS 10 因苹果健康导致闪退 crash-b
如果在app中调用了苹果健康,iOS10中会出现闪退.控制台报出的原因是: Terminating app due to uncaught exception 'NSInvalidArgumentEx ...
- 历时一周,unity3d+xtion打造我的第一个休闲体感小游戏《空降奇兵》
1.游戏介绍 本游戏属于休闲小游戏,主要操作如下: 菜单控制:举起左手或右手,点击左边或者右边的菜单:挥动左手或右手,选择关卡: 操作方式:玩家跳跃,游戏中的伞兵从飞机开始降落:玩家通过控制伞兵的左右 ...
- Careercup - Facebook面试题 - 4909367207919616
2014-05-01 01:23 题目链接 原题: WAP to modify the array such that arr[I] = arr[arr[I]]. Do this in place i ...
- Careercup - Facebook面试题 - 6299074475065344
2014-05-01 01:00 题目链接 原题: Given a matrix with 's. What is the maximum area of the rectangle. In . Ho ...
- 关于json的知识整理
一.什么是json JSON:JavaScript 对象表示法(JavaScript Object Notation). JSON 是存储和交换文本信息的语法.类似 XML,但JSON 比 XML 更 ...
- JXL读取写入excel表格数据
问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...