Remove Duplicates from Sorted List :

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

public ListNode deleteDuplicates(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode temp = head;
while (temp.next != null) {
if (temp.next.val == temp.val) {
temp.next = temp.next.next;
}
else
{
temp = temp.next;
}
}
return head;
}

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,这个是把所有重复元素都删掉,一个不留,所有要用两重while去重,上面一题去重时只需要一重循环。

public ListNode deleteDuplicates2(ListNode head)
{
if(head == null || head.next == null)
{
return head;
}
ListNode pre = new ListNode(0);//记录头结点,因为开头如果有重复元素,head将改变
pre.next = head;
ListNode temp = pre;
while (temp.next != null && temp.next.next != null) {
if(temp.next.val == temp.next.next.val)
{
int dup = temp.next.val;
while(temp.next != null && temp.next.val == dup)//去掉所有重复元素
{
temp.next = temp.next.next;
}
}
else
{
temp = temp.next;
}
}
return pre.next;
}

Remove Duplicates from Sorted List ,除去链表中相邻的重复元素的更多相关文章

  1. Remove Duplicates from Sorted List 去除链表中重复值节点

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点

    给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...

  3. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  4. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  5. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  6. [LeetCode]83. Remove Duplicates from Sorted List(排序链表去重)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. Remove Duplicates from Sorted List (链表)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  8. [LeetCode] Remove Duplicates from Sorted List II 链表

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

  9. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

随机推荐

  1. java代理与动态代理的学习

    静态代理比较简单,就是代理对象与被代理对象继承相同的接口,代理类负责调用被代理类(委托类)的对象的相关方法去提供具体的服务,一个代理类只能为一个接口服务,要是有很多服务的话需要开很多代理类.而动态代理 ...

  2. Less-css预处理Node and VS扩展编译

    node编译 第一步:https://nodejs.org/en/  到node官网下载最新的node 第二步:和普通软件一样把node安装好 第三步:运行-cmd,准备安装less 全局安装(整个电 ...

  3. node.js开发学习一HelloWorld

    前言:由于公司业务需求,最近启动了node.js的开发任务,想把自己的开发学习历程记录记录下来,可以增加记忆,也方便查找.虽然对javascript有一定的了解,但是刚接触node.js的时候,发现还 ...

  4. IO流入门-第三章-FileInputStream_FileOutputStream复制

    利用FileInputStream和FileOutputStreamj进行复制粘贴 /* 文件复制粘贴 */ import java.io.*; public class FileInput_Outp ...

  5. 服务不支持 chkconfig 的解决方法

    系统服务,在chkconfig --add  servername的时候老是提示服务不支持 chkconfig,经过查找,解决办法如下. 示例,auto_run的前三行如下:#!/bin/sh#chk ...

  6. 【opencv安裝】ubuntu16 opencv安装+测试

    ubuntu16.04 install opencv2.4 to python2 and c++ 四大主流库比较: 对OpenCV的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善, ...

  7. selectedIndex返回被选中的option的index.

    / <label for="city">城市</label> <select id="city" name="schoo ...

  8. kubestack 源码分析

    简介:KubeStack is an OpenStack network provider for kubernetes.KubeStack is devided into two functions ...

  9. JVM虚拟机—JVM的垃圾回收机制(转载)

    1.前言 理解JVM的垃圾回收机制(简称GC)有什么好处呢?作为一名软件开发者,满足自己的好奇心将是一个很好的理由,不过更重要的是,理解GC工作机制可以帮助你写出更好的Java程序. 在学习GC前,你 ...

  10. Part01、memcache 缓存

    Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy 目录 一. Memcached Memcached安装和基本使用 Python操作Memcached2.1 se ...