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. jQuery animate() - 使用相对值 在值的前面加上 += 或 -=

    也可以定义相对值(该值相对于元素的当前值).需要在值的前面加上 += 或 -=: 实例 $("button").click(function(){ $("div" ...

  2. Powershell&.NET数值取整处理

    如何取一个数的整数值? 使用类型强制转换 Powershell的强制转换有2种方式,一种是直接类型强制转换,另一种是通过-as运算符进行转换 PS F:\> [int] (3 / 2) # 直接 ...

  3. 在Sql Server中使用证书加密数据

    IF NOT EXISTS () CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'QWE23987zxJKL569&agf1$94467GRkjg5k3 ...

  4. java基础10 吃货联盟点餐系统

    public class OrderMsg { public static void main(String[] args) throws Exception { /** * 订餐人姓名.选择菜品.送 ...

  5. ugui中实现圆形按钮

    实现圆形按钮,原本是使用 alphHitTestMinimumThreshold 改成重载IsRaycastLocationValid来实现,直接贴代码 using UnityEngine; usin ...

  6. Django模板继承后出现logo图片无法加载的问题

    父文件:index.html <!DOCTYPE html> <html lang="en"> <head> <title>{% b ...

  7. 同一TextView上内容的不同显示(最新)-SpannableString

    上次发了一篇同一TextView内容的不同显示这篇文章. 有关颜色的不同显示,我使用了最简单可是也最复杂的方法.忘记使用SpannableString,现又一次发一下,大家參考下. TextView组 ...

  8. N多条短信,用什么算法从中找出相似内容的来?

    创建树,每个字符为一个节点,对于同一位置字符相同的共用一个节点.最后找出具有公共节点的短信.例如:MessageA "hello,world"MessageB "hell ...

  9. window7+wamp环境配置Oracle数据库连接

    最近开发需要使用的oracle数据库!翻看了PHP手册,也在网上找了些帖子!功夫不负有心人,花费了四五个小时的时间,终于找到了Oracle的配置方法.下面就讲解下如何配置Oracle数据库连接吧! 附 ...

  10. 转:.NET特性与反射

    .NET编译器的任务之一是为所有定义和引用的类型生产元数据描述.除了程序集中标准的元数据外,.NET平台允许程序员使用特性(attribute)把更多的元数据嵌入到程序集中.简而言之,特性就是用于类型 ...