[LeetCode][Java] 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->2->3->3->4->4->5 。返回1->2->5.
给定1->1->1->2->3 ,返回2->3.
算法分析:
设置前后双指针。后指针遇到反复元素就一直遍历直到反复的结尾,之后前指针指向后指针,这样就略过全部的反复元素。
AC代码:
<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
public ListNode deleteDuplicates( ListNode head)
{
ListNode pre;
ListNode cur;
ListNode newhead = new ListNode(0);
newhead.next=head;
if(head==null||head.next==null)
return head;
pre=newhead;
cur=head;
while(cur.next!=null)
{
if(cur.next.val==cur.val)//处理头几个元素同样的样例 如1 1 1 2 3 4
{
while(cur.next.val==cur.val)
{
cur=cur.next;
if(cur.next==null)//处理末尾几个元素同样的样例 1 2 3 4 5 5 5
break;
}
pre.next=cur.next;
//pre=pre.next;
cur=cur.next;
if(cur==null)
break;
}
else//处理头几个元素不同样的样例 1 2 3 4 5
{
pre=pre.next;
cur=cur.next;
} }
return newhead.next;
}
}</span>
[LeetCode][Java] Remove Duplicates from Sorted List II的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [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】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [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] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- 使用html2canvas实现网页截图,并嵌入到PDF
使用html2canvas实现网页截图并嵌入到PDF 以前我们只能通过截图工具进行截取图像.这使得在业务生产中,变得越来越不方便.目前的浏览器功能越来越强大,H5也逐渐普及,浏览器也可以实现截图了.这 ...
- docker 离线安装
适用于: 1.内网安装docker 2.内网升级docker debian 8 sudo apt-get updatesudo apt-get install -d apt-transport-htt ...
- jquery实现密码强度检测
jquery实现密码强度验证 jquery实现密码强度验证 JS代码: $('#pass').keyup(function(e) { var strongRegex = new RegExp( ...
- Django中对接第三方支付(支付宝)实现支付的流程
1. 业务逻辑准备 1. 使用沙箱提供的商家环境 沙箱环境:是支付宝提供给开发者的模拟支付的环境 沙箱应用:https://docs.open.alipay.com/200/105311 沙箱账号:h ...
- ES6(函数新增特性)
ES6(函数新增特性) 1.函数参数默认值 没有 y 时,默认就是world 有 y 时,输出值即可 (错误) (C有默认值,正确) 默认值后面不能再有没有默认值的变量 2.作用域 y 取其前面的 x ...
- BigTable
Bigtable发布于2006年,启发了无数的NoSQL数据库,比如:Cassandra.HBase等等. Cassandra架构中有一半是模仿Bigtable,包括了数据模型.SSTables以及提 ...
- Go函数学习
package main import ( "fmt" "reflect" "runtime" "math" ) //函 ...
- server中intersect的用法
intersect 就像数学中的交集一样, select nam from tb_table1 intersect select name from tb_table2 查询的是两个数据集的交集 ...
- [codevs1050]棋盘染色 2
[codevs1050]棋盘染色 2 试题描述 有一个5*N的棋盘,棋盘中的一些格子已经被染成了黑色,你的任务是对最少的格子染色,使得所有的黑色能连成一块. 输入 第一行一个整数N(<=100) ...
- HTTP API 自动化测试从手工测试到平台的演变
不管是 Web 系统,还是移动 APP,前后端逻辑的分离设计已经是常态化,相互之间通过 API 调用进行数据交互.在基于 API 约定的开发模式下,如何加速请求 / 响应的 API 测试,让研发人员及 ...