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. [P1580] yyy loves Easter_Egg I

    Link: P1580 传送门 Solution: 拿来练练字符串的读入: 1.$gets()$相当于$c++$中的$getline()$,但返回值为指针!(无数据时为NULL) (都读入换行符,并将 ...

  2. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  3. 【kmp算法】poj2185 Milking Grid

    先对每行求出所有可能的循环节长度(不需要整除). 然后取在所有行中都出现了的,且最小的长度为宽. 然后将每一行看作字符,对所有行求next数组,将n-next[n](对这些行来说最小的循环节长度)作为 ...

  4. 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流

    #include<cstdio> #include<cstring> #include<algorithm> #include<queue> using ...

  5. xcode中一些便捷用法@literals简写

    总结一下,新的属性绑定规则如下: ●  除非开发者在实现文件中提供getter或setter,否则将自动生成 ● 除非开发者同时提供getter和setter,否则将自动生成实例变量 ●  只要写了s ...

  6. Saga的实现模式——进化(Saga implementation patterns – variations)

    在之前的几个博客中,我主要讲了两个saga的实现模式: 基于command的控制者模式 基于事件的观察者模式 当然,这些都不是实现saga的唯一方式.我们甚至可以将这些结合起来. 发布者——收集者 回 ...

  7. Linux内核转发技术

    前言 在linux内核中,通常集成了带有封包过滤和防火墙功能的内核模块, 不同内核版本的模块名称不同, 在2.4.x版本及其以后的内核中, 其名称为iptables, 已取代了早期的ipchains和 ...

  8. VMware虚拟机中为Linux 添加虚拟硬盘(VirtualBox方法类似)

    修改1:2014-06-24 11:38:21 Linux添加硬盘是在原来安装的硬盘空间不够或者需要使用其他硬盘上的东西时候的解决办法,因为大多数初学者习惯使用虚拟机,这里以在Vmware虚拟机中实现 ...

  9. shell产生随机数七种方法

    shell实例浅谈之三产生随机数七种方法   一.问题 Shell下有时需要使用随机数,在此总结产生随机数的方法.计算机产生的的只是“伪随机数”,不会产生绝对的随机数(是一种理想随机数).伪随机数在大 ...

  10. Office 2007 SP3 正试版补丁包下载

    这仅仅是下载补丁包.下载地址也是微软官网的.以下是MD5是我下载后校验的. 2007 Microsoft Office 套件 Service Pack 3 (SP3) 文件名: office2007s ...