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 ...
随机推荐
- uC/OS-II邮箱(mbox)块
/*************************************************************************************************** ...
- 服务器配置ssl证书支持苹果ATS方法
服务器配置ssl证书支持苹果ATS方法 发布日期:2016-12-14 苹果安全工程&架构部门主管Ivan Kristic表示ATS将在今年底成为App Sotre app的必要条件,这将大幅 ...
- netty 解决TCP粘包与拆包问题(一)
1.什么是TCP粘包与拆包 首先TCP是一个"流"协议,犹如河中水一样连成一片,没有严格的分界线.当我们在发送数据的时候就会出现多发送与少发送问题,也就是TCP粘包与拆包.得不到我 ...
- springmvc @responseBody自动打包json出现错误(外键查询死循环)问题
在外键字段的get方法上加入@JsonIgnore
- Java——菜单组件
import java.awt.Container; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; i ...
- Eclipse中修改Web项目的URL访问路径
背景 访问路径,也就是指在浏览器中访问该web系统时的根路径,比如http://localhost:8080/xxxx/index.jsp 这里的xxxx,也就是request.getContext ...
- PCH 配置
$(SRCROOT)/$(PROJECT)/PrefixHeader.pch
- Intent启动一个新的页面
一,Intent(目的) 的分类 显式 Intent 构造函数重载之一: Intent intent = new Intent(FirstActivity.this,SecondActivity.cl ...
- SPL--Serializable
Serializable[自定义序列化的接口] : 实现此接口的类将不再支持 __sleep() 和 __wakeup(). 作用: 为一些高级的序列化场景提供支持.__sleep()和__wakeu ...
- Apache Maven 入门篇 ( 上 )
作者:George Ma 写这个 maven 的入门篇是因为之前在一个开发者会的动手实验中发现挺多人对于 maven 不是那么了解,所以就有了这个想法. 这个入门篇分上下两篇.本文着重动手,用 mav ...