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

Example 1:

Input: 1->1->2
Output: 1->2

Example 2:

Input: 1->1->2->3->3
Output: 1->2->3

这道题让我们移除给定有序链表的重复项,那么可以遍历这个链表,每个结点和其后面的结点比较,如果结点值相同了,只要将前面结点的 next 指针跳过紧挨着的相同值的结点,指向后面一个结点。这样遍历下来,所有重复的结点都会被跳过,留下的链表就是没有重复项的了,代码如下:

解法一:

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode *cur = head;
while (cur && cur->next) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
} else {
cur = cur->next;
}
}
return head;
}
};

我们也可以使用递归的方法来做,首先判断是否至少有两个结点,若不是的话,直接返回 head。否则对 head->next 调用递归函数,并赋值给 head->next。这里可能比较晕,先看后面一句,返回的时候,head 结点先跟其身后的结点进行比较,如果值相同,那么返回后面的一个结点,当前的 head 结点就被跳过了,而如果不同的话,还是返回 head 结点。可以发现了,进行实质上的删除操作是在最后一句进行了,再来看第二句,对 head 后面的结点调用递归函数,那么就应该 suppose 返回来的链表就已经没有重复项了,此时接到 head 结点后面,在第三句的时候再来检查一下 head 是否又 duplicate 了,实际上递归一直走到了末尾结点,再不断的回溯回来,进行删除重复结点,参见代码如下:

解法二:

class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next) return head;
head->next = deleteDuplicates(head->next);
return (head->val == head->next->val) ? head->next : head;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/83

类似题目:

Remove Duplicates from Sorted List II

参考资料:

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

https://leetcode.com/problems/remove-duplicates-from-sorted-list/discuss/28614/My-pretty-solution.-Java.

https://leetcode.com/problems/remove-duplicates-from-sorted-list/discuss/28625/3-Line-JAVA-recursive-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项的更多相关文章

  1. [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项

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

  2. LeetCode 83. Remove Duplicates from Sorted List (从有序链表中去除重复项)

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

  3. 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)

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

  4. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  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 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

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

  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] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

随机推荐

  1. “为什么DirectX里表示三维坐标要建一个4*4的矩阵?”

    0x00 前言 首先要说明的是,本文的标题事实上来自于知乎上的一个同名问题:为什么directX里表示三维坐标要建一个4*4的矩阵? - 编程 .因此,正如Milo Yip大神所说的这个标题事实上是存 ...

  2. Dapper-据说stackoverflow使用的orm

    using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; usin ...

  3. 在Application_Error事件中获取当前的Action和Control

    ASP.NET MVC程序处理异常时,方法有很多,网上也有列举了6种,下面是使用全局处理在Global.asax文件的Application_Error事件中实现.既然是ASP.NET MVC,我需要 ...

  4. Asp.net 面向接口可扩展框架之业务规则引擎扩展组件

    随着面向接口可扩展框架的继续开发,有些功能开发出现了"瓶颈",有太多的东西要写死才好做.但写死的代码扩展性是非常的不好,迷茫中寻找出入... 进而想到我以前开发的好几个项目,都已有 ...

  5. pwm 占空比 频率可调的脉冲发生器

    module xuanpin #(parameter N=25)(clk,clr,key_in_f,key_in_z,f_out);input clk,clr,key_in_f,key_in_z;ou ...

  6. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method

    异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...

  7. SQLServer修改表字段名称

    EXEC sp_rename 'TableName.[ColumnName]','ColumnNew','COLUMN'

  8. H5 本地存储一

    localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...

  9. SVG入门

    这部分包括三个内容: 1. 基本图形 2. 基本属性 3. 基础操作API 基本图形 名称 描述 参数 图示 rect 定义一个矩形 x="矩形的左上角的x轴" y="矩 ...

  10. jQuery选择器笔记

    1.$(this).hide() - 隐藏当前元素 $("p").hide() - 隐藏所有段落 $(".test").hide() - 隐藏所有 class= ...