这道题本质上不难,难的是细节处理,容易出错。

第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过。

Remove Duplicates from Sorted List

My Submissions

Question
Total Accepted: 90731 Total Submissions: 255705 Difficulty: 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.

下面是Discuss里面的好代码:只有三行!!!

Java写法

 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;
}

另一个好代码,和我的思想差不多,不过更简洁:

 public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return head; ListNode cur = head;
while(cur.next != null) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next;
}
else cur = cur.next;
}
return head;
}
}
 然后是我自己写的:
C语言
 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
if (head == NULL)
return NULL;
if (head->next == NULL)
return head; struct ListNode* p = head;
struct ListNode* q = p->next;
while(p->next != NULL) {
if(p->val == p->next->val) {
p->next = p->next->next;
}
else if(p->next != NULL)
p = p->next;
} return head;
}

【11_83】Remove Duplicates from Sorted List的更多相关文章

  1. 【26】Remove Duplicates from Sorted Array

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

  2. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  3. 【leetcode】Remove Duplicates from Sorted Array I & II(middle)

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

  4. 【数组】Remove Duplicates from Sorted Array II

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  5. 【Leetcode】【Medium】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  6. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  7. 【Leetcode】【Easy】Remove Duplicates from Sorted Array

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

  8. 【leetcode】Remove Duplicates from Sorted List

    题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  9. 【leetcode】 Remove Duplicates from Sorted List

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

随机推荐

  1. [转载]Macaca 测试 Android 应用:UIAutomator

    在用macaca进行自动化测试,想试一下移动端测试,看到这篇文章,尝试一下. 前言 用 Macaca 可以快速.便捷地进行安卓 native 的自动化测试,用简洁的 js 语法,写下用例,然后执行 M ...

  2. 华为RH2285安装过程及经验总结

    安装测试 1.由于服务器为二手服务器,噪音相对较大,如果未经改造无法正常使用,当机器第一次运行的时候,我的血压一下升高不少. 第一步  服务器的远程端口和路由器连接在一起 第二步  设置服务器的bio ...

  3. Spark Streaming源码解读之Receiver生成全生命周期彻底研究和思考

    本期内容 : Receiver启动的方式设想 Receiver启动源码彻底分析 多个输入源输入启动,Receiver启动失败,只要我们的集群存在就希望Receiver启动成功,运行过程中基于每个Tea ...

  4. python与正则表达式:re模块详解

    re模块是python中处理正在表达式的一个模块 正则表达式知识储备:http://www.cnblogs.com/huamingao/p/6031411.html 1. match(pattern, ...

  5. vi的查找与替换

    0x01 查找 (在命令行模式下) /<要查找的字符>   向下查找要查找的字符 ?<要查找的字符>   向上查找要查找的字符 0x02 替换 (在底行模式下) :0,$s/s ...

  6. spark Mllib基本功系列编程入门之 SVM实现分类

    话不多说.直接上代码咯.欢迎交流. /** * Created by whuscalaman on 1/7/16. */import org.apache.spark.{SparkConf, Spar ...

  7. session基础

    1.每个页面都必须开启session_start()后才能在每个页面里面使用session. 2.session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端(是基 ...

  8. 设置PATH变量

    一不小心把PATH变量清空了,所有的命令都执行不了了,提示“xxx: command not found”,解决办法:在命令行输入export PATH=/usr/local/sbin:/usr/lo ...

  9. android系统中查看哪些端口被哪些应用打开

    1 查看哪些端口开放,netstat 2 根据端口号获取到UID,比如端口号为10050,转成16进制是2742,使用命令grep -i 2742 /proc/net/tcp6,就能看到其UID,假如 ...

  10. C++中使用初始化列表比在构造函数中对成员变量赋值更高效

    这是在面试中遇到的一个问题,没有答出来,后来上网上查了一些资料,终于弄明白了: 一.首先c++标准规定成员变量必须在调用构造函数前进行初始化(这一点很重要) 二.如果我们在构造函数中对成员变量进行初始 ...