83. Remove Duplicates from Sorted List【easy】

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.

解法一:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
} ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head; while (head != NULL && head->next != NULL) {
if (head->val == head->next->val) {
ListNode * temp = head->next;
head->next = temp->next;
free(temp);
}
else {
head = head->next;
}
} return dummy->next;
}
};

这里也可以不用dummy节点,之所以那么写就是为了好看而已,嘿嘿

解法二:

 public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}

参考了@wen587sort 的代码,大神的代码也是受到了下面的提示:

This solution is inspired by renzid https://leetcode.com/discuss/33043/3-line-recursive-solution

题外话,关于递归,我觉得@ElayMarco 的说法很中立:

Well, on sites like these one's, I believe a lot of times people want to post 'other ways' of doing things, which is good. Recursive solutions sometimes require less coding to implement, and as a by-product of this look more clever or elegant. Recursion is just a tool. Sometimes the job calls for a hammer. Other times, a hammer is not suitable. Part of being a good programmer is knowing when to use which tools. But on sites like these, some people like to 'sharpen their tools' by writing recursive solutions to problems where iterative solutions are more efficient. Think of it as practice.

看来,还是我自己太菜了……

83. Remove Duplicates from Sorted List【easy】的更多相关文章

  1. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  2. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  3. leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)

    203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...

  4. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 【一天一道LeetCode】#83. Remove Duplicates from Sorted List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  8. 【LeetCode】83 - Remove Duplicates from Sorted Array

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

  9. 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...

随机推荐

  1. [BZOJ1143][CTSC2008]祭祀river(Dilworth定理+二分图匹配)

    题意:给你一张n个点的DAG,最大化选择的点数,是点之间两两不可达. 要从Dilworth定理说起. Dilworth定理是定义在偏序集上的,也可以从图论的角度解释.偏序集中两个元素能比较大小,则在图 ...

  2. [HDU5739]Fantasia(圆方树DP)

    题意:给一张无向点带有权无向图.定义连通图的权值为图中各点权的乘积,图的权值为其包含的各连通图的权和.设z_i为删除i点后图的权值,求$S = (\sum\limits_{i=1}^{n}i\cdot ...

  3. [BZOJ 1145] 图腾totem

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=1145 Solution: 算是一道神题了吧 设 f(abcd)为:当选出的四个数相对大小 ...

  4. [Contest20180415]看无可看

    题意:有一个数列$f$,对$\forall i\geq2,f_i=2f_{i-1}+3f_{i-2}$,给定$f_0,f_1$,再给定一个集合$S=\{a_{1\cdots n}\}$和$k$,求$\ ...

  5. [Contest20180405]抑制「超我」

    古明地恋(koishi)和计算器(calculator)是好朋友.恋恋有一个神奇的计算器,可以进行两个数在模$2^n$意义下的加法运算.计算器上有一个寄存器,一开始寄存器中的数为$0$,每当恋恋输入一 ...

  6. 【暴力】Codeforces Round #398 (Div. 2) A. Snacktower

    题意不复述. 用个bool数组记录一下,如果某一天,当前剩下的最大的出现了的话,就输出一段. #include<cstdio> using namespace std; int n; bo ...

  7. 【kd-tree】bzoj3290 Theresa与数据结构

    离线所有操作,对所有可能存在的点建立kd-tree,add相当于权值+1,cancel相当于权值-1. 修改操作要记录kd-tree上每个点的fa,从底向上地进行修改. 优化:若一个矩形框的sumv= ...

  8. SQL函数学习(十六):STUFF()函数

    16.STUFF()函数 STUFF()函数用于删除指定长度的字符,并可以在制定的起点处插入另一组字符. 16.1 STUFF()函数语法 select stuff(列名,开始位置,长度,替代字符串) ...

  9. Java生成扫描可以生成手机号名片的二维码

    (1)需求:用户通过扫描pc端网站后台管理系统的二维码获取对接人的相关信息,主要是是手机号信息,达到点击可以直接打电话或者将对接人的信息直接保存到通讯录 注:源码来源:https://blog.csd ...

  10. 移植Linux2.6.38到Tiny6410_1GNandflash

    首先说一下为什么要下决心移植Linux内核,本来移植完Uboot后我想先把韦东山第二期的驱动教程看完后在试图移植内核,可是当我跟着教程写好LCD的驱动后,去设置编译友善的内核来测试程序时,我稍微改一点 ...