题意:

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的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. [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 ...

  3. [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 ...

  4. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  5. [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% ...

  6. [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)

    排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...

  7. LeetCode OJ Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  9. [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 ...

随机推荐

  1. VR技术在数据中心3D机房中的应用 (下)

    VR技术在数据中心3D机房中的应用 (下) 前面给大家简单科普了一下VR的硬件设备以及VR在各个领域的应用,是不是觉得非常高大上?千言万语概括成一句话,VR能给用户带来前所未有的沉浸感和交互方式,让人 ...

  2. sql实验

    数据表xiami_1,结构如下: CREATE TABLE xiami_1( id ) not null auto_increment, singer ) not null, title ) not ...

  3. 条款33:避免遮掩继承而来的名称(Avoiding hiding inherited names)

    NOTE: 1.derived classes 内的名称会遮掩base classes内的名称.在public继承下从来没有人希望如此. 2.为了让被遮掩的名称再见天日,可使用using 声明方式或转 ...

  4. php 链接 mongodb

    1.下载phpmongodb 驱动:https://s3.amazonaws.com/drivers.mongodb.org/php/index.html 2.Precompiled binaries ...

  5. 性能测试工具 - ab 简单应用

    之前知道一般网站性能可以通过 LoadRunner, JMeter, QTP 等相应的软件进行测试, 印象中本科学习 “软件测试” 这门课程时安装并使用过, LoadRunner等不是一个小软件, 安 ...

  6. 如何使用werkzeug创建WSGI APP

    注意 : 1.定义__call__的意义 class App(): def __init__(self): pass def method(self): pass app=App() app() #错 ...

  7. 76. Spring Boot完美解决(406)Could not find acceptable representation原因及解决方法

    [原创文章]        使用Spring Boot的Web项目,处理/login请求的控制器方法(该方法会返回JSON格式的数据).此时如果访问localhost:8080/login.html, ...

  8. CodeForces - 592D Super M 题解

    题目大意: 一棵树 n个点 有m个点被标记 求经过所有被标记的点的最短路径的长度以及起点(如有多条输出编号最小的起点). 思路: 1.当且仅当一个点本身或其子树中有点被标记时该点在最短的路径上因此,可 ...

  9. POJ 1379 (随机算法)模拟退火

    题目大意: 给定一堆点,找到一个点的位置使这个点到所有点中的最小距离最大 这里数据范围很小,精度要求也不高,我们这里可以利用模拟退火的方法,随机找到下一个点,如果下一个点比当前点优秀就更新当前点 参考 ...

  10. Java SSH框架系列:用户登录模块的设计与实现思路

    1.简介用户登录模块,指的是根据用户输入的用户名和密码,对用户的身份进行验证等.如果用户没有登录,用户就无法访问其他的一些jsp页面,甚至是action都不能访问.二.简单设计及实现本程序是基于Jav ...