leetcode 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
.
题意:
给出一个链表,删除所有重复的结点。
步骤:
(1)定义一个新的头结点,用来指向head,并将head向前移动一位。
1 -> 1 -> 1 -> 2 -> 3
变为 head -> 1 -> 1 -> 1 -> 2 -> 3
(2)遍历各结点,每次比较两个结点,head.next和head.next.next.
1. 如果head.next.val == head.next.next.val, 两个结点的值相等,用temp记录这个值,
从next开始依次遍历删除每个值为temp的结点,head.next = head.next.next.
2. 如果next和next.next的值不相等,继续向后遍历,head =head.next;
/**
* 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) return null; ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy; while(head.next != null && head.next.next != null){
if(head.next.val == head.next.next.val){
int temp = head.next.val;
while(head.next != null && head.next.val == temp){
head.next = head.next.next;
}
}else{
head = head.next;
}
}
return dummy.next; }
}
leetcode 82. Remove Duplicates from Sorted List II的更多相关文章
- [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 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [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 (删除排序链表的反复II) 解题思路和方法
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 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 ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 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】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
随机推荐
- oc必须知道的知识点
id数据类型 1.通用的指针类型 2.没有*号 3.使用id类型时,不能给对象的属性或成员变量进行赋值 4.可以对其发送任何(存在的)消息 import与@class的区别 1.import会包含 ...
- eshop截取字符串长度 和去掉省略号
<!-- {if $goods.goods_brief} --> {$goods.goods_brief|truncate:17}<!-- {/if} --> 去掉省略号: 找 ...
- ajax传输 基础一
一个简单页面的传输 index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- new和alloc init的区别
背景说明,new是较为老式的写法,后来发现只有一个new不好使,才引入了alloc和init这种写 法,保留new一是向后兼容,二是很多时候是一种更简单的写法.其实是一样的,new在内部调用 的all ...
- PHP file_get_contents设置超时处理方法
从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_c ...
- maven test 运行 指定类或方法 打包 mvn clean assembly:assembly
>mvn test -Dtest=[ClassName] 运行测试类中指定的方法:(这个需要maven-surefire-plugin:2.7.3以上版本才能支持) >mvn test ...
- zabbix特性
在知道zabbix是什么之后,我们最关心的是zabbix有什么特性,了解特性之后,我们才能决定是否会使用zabbix,以及zabbix是否适合我们. 概述 Zabbix是一个高度集成的网络监控套件,通 ...
- html5入门
1.canvas标签 <canvas id="myCanvas"></canvas><!--canvas标签定义图形,比如图标和其他图像--> ...
- YII2项目常用技能知识总结
1.不通过日志获取AR执行的原生SQL语句和打印变量数据 $query = User::find() ->select(['username'])->where(['id'=>[1, ...
- Promise 异步(asynchronous )编程
概述 Promise.all(iterable) 方法返回一个promise,该promise会等iterable参数内的所有promise都被resolve后被resolve,或以第一个promis ...