[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 ...
随机推荐
- 从零开始--系统深入学习Android
http://www.cnblogs.com/tianjian/category/354587.html
- luogu P2078 朋友
题目背景 小明在A公司工作,小红在B公司工作. 题目描述 这两个公司的员工有一个特点:一个公司的员工都是同性. A公司有N名员工,其中有P对朋友关系.B公司有M名员工,其中有Q对朋友关系.朋友的朋友一 ...
- 一篇文章掌握nightwatch自动化测试
nightwatch.js是一个web-ui自动化测试框架,被vue-cli深度整合进来.如果一个项目是基于vue-cli搭建的,基本可以做到开箱即用. 但是我们不可能一直都使用vue-cli.因为它 ...
- Python旅途——数据类型
1.概要 今天跟大家分享的是数据类型,数据类型在Python中的重要程度就好比我们汉语中的拼音一样重要. 2.数据类型 1.整形 整形就像是数学中的1.2.3一样,用int来表示. a = 1 # ...
- LeetCode(16)3Sum Closest
题目 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 「LibreOJ β Round #3」绯色 IOI(抵达)
[题解] 我们可以发现叶子节点的关联点一定是它的父亲节点,那么我们dfs一遍就可以求出所有节点的关联点,或者判断出无解. 对于每个点i,它的关联点u的危险度肯定比它连接的其他点vi的危险度小,我们从u ...
- 杭电 2553 N皇后问题 (dfs)
Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合 ...
- 【】关闭QQ右下角各种弹框
[]关闭QQ右下角弹框 一: 二: 超级会员设置过滤(屏蔽)广告后可以过滤哪些广告? 1.可以过滤QQ客户端好友聊天对话框右侧出现的Flash广告.左下角的文案广告: 如图: 2.可以 ...
- 关于构造函数什么值传递给他的实例,只有this和prototype
var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype.cc=&quo ...
- String字符串去掉双引号
public static String stringReplace(String str) { //去掉" "号 String str= str.replace("\& ...