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

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

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. oracle工作经验(左右连接、decode)

    oracle左右连接:select a.studentno, a.studentname, b.classname from students a, classes b where a.classid ...

  2. c/c++ 对象内存布局

    一.对象内存查看工具 VS 编译器 CL 的一个编译选项可以查看 C++ 类的内存布局,非常有用.使用如下,从开始程序菜单找到 Visual Stdio 2012. 选择 VS 的命令行工具,按如下格 ...

  3. html嵌套表格示例

    常用嵌套表格示例,出自<网页开发手记:HTML+CSS+JavaScript实战详解>   <html>   <head>   <title>嵌套表格布 ...

  4. Gradient Boosting Decision Tree学习

    Gradient Boosting Decision Tree,即梯度提升树,简称GBDT,也叫GBRT(Gradient Boosting Regression Tree),也称为Multiple ...

  5. delphi软件启动的顺序解密。

    运行顺序 1.主窗体的oncreate -- onshow ---- onActivate ---- onResize --- 然后继续走,这个时候主窗体已经显示出来了,猜想delphi的思路是先让主 ...

  6. Web前端之html_day1

    1.html结构 1 2 3 4 5 6 7 8 9 10 <!DOCTYPE html>   <html lang="en"> <head> ...

  7. 创建maven项目时,提示 overlaps the workspace location

    创建maven项目时提示: Invalid project description.OKF:\java\mywork overlaps the workspace location: F:\java\ ...

  8. 一个很酷的加载loading效果--IT蓝豹

    一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...

  9. web应用中对配置文件的包装

    <bean id="placeholderConfig" class="com.shz.utils.AdvancedPlaceholderConfigurer&qu ...

  10. nginx限制ip连接数和带宽

    今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了.还要看一下配置才想起了.那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有.下 ...