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

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

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写法

1 public ListNode deleteDuplicates(ListNode head) {
2         if(head == null || head.next == null)return head;
3         head.next = deleteDuplicates(head.next);
4         return head.val == head.next.val ? head.next : head;
5 }

View Code

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

 1 public class Solution {
 2     public ListNode deleteDuplicates(ListNode head) {
 3         if (head == null) return head;
 4
 5         ListNode cur = head;
 6         while(cur.next != null) {
 7             if (cur.val == cur.next.val) {
 8                 cur.next = cur.next.next;
 9             }
10             else cur = cur.next;
11         }
12         return head;
13     }
14 }

View Code

然后是我自己写的:
C语言
 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 struct ListNode* deleteDuplicates(struct ListNode* head) {
 9     if (head == NULL)
10         return NULL;
11     if (head->next == NULL)
12         return head;
13
14     struct ListNode* p = head;
15     struct ListNode* q = p->next;
16     while(p->next != NULL)  {
17         if(p->val == p->next->val)  {
18             p->next = p->next->next;
19         }
20         else if(p->next != NULL)
21             p = p->next;
22     }
23
24     return head;
25 }

转载于:https://www.cnblogs.com/QingHuan/p/5051388.html

【11_83】Remove Duplicates from Sorted List相关推荐

  1. 【leetcode】Remove Duplicates from Sorted Array

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

  2. 【Leetcode】Remove Duplicates from Sorted List II

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

  3. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

  4. 【LeetCode OJ】Remove Duplicates from Sorted List

    2019独角兽企业重金招聘Python工程师标准>>> Given a sorted linked list, delete all duplicates such that eac ...

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

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

  6. LeetCode:Remove Duplicates from Sorted List I II

    LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...

  7. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  8. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  9. 2016.5.57—— Remove Duplicates from Sorted List

    Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...

最新文章

  1. win7多国语的安装说明
  2. house price model
  3. 加油站(暴力+贪心)
  4. Git简单基本操作指令集合
  5. MQTT和HTTP的区别
  6. 多项新政催生本年度购房最佳“窗口期”
  7. 小动画制作 图片盒子配合定时器 winform 114869633
  8. 【impala】impala的shell命令使用
  9. WPF RichTextBox设置文本颜色
  10. 解决GBK字符转UTF-8乱码问题
  11. ADF12C UI根据row重定位VO的当前行
  12. 2016-06-07【普元EOS Studio】
  13. ​网红拉姆之死,这辈子最看不起这种男人!
  14. 故障树FTA(fault tree analysis)工具链
  15. alook浏览器_Alook浏览器app下载|Alook浏览器安卓版下载 v3.4.1 - 跑跑车安卓网
  16. 计算机毕业设计ssm民宿预定管理系统20sxh系统+程序+源码+lw+远程部署
  17. hypermesh和lsdyna联合仿真计算某汽车座椅进行的头冲吸能实验
  18. Gephi快速入门(一):Windows下安装Gephi
  19. 大学计算机实验六实验报告,大学计算机实验6 实验报告.pdf
  20. 火车头采集器V10下载-火车头采集器免费

热门文章

  1. SQL Server中的Union和Union All语句之间的差异及其性能
  2. 从零点五开始用Unity做半个2D战棋小游戏(七)
  3. 【UE灯光•简介】UE4光照类型和灯光参数
  4. 小游戏掉帧卡顿启动慢运行内存不足……这些问题有解吗?
  5. PMP每日三题(2022年2月15日)
  6. 1024|程序员节来了!
  7. Python_面向对象_递归
  8. pandas read_csv 出现中文乱码
  9. 服务器性能瓶颈分析方法
  10. Gmail POP3设置