【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错。
第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过。
Remove Duplicates from Sorted List
My Submissions
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
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相关推荐
- 【leetcode】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- 【Leetcode】Remove Duplicates from Sorted List II
问题: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【Leetcode】Remove Duplicates from Sorted Array II
题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...
- 【LeetCode OJ】Remove Duplicates from Sorted List
2019独角兽企业重金招聘Python工程师标准>>> Given a sorted linked list, delete all duplicates such that eac ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array
LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...
- 2016.5.57—— Remove Duplicates from Sorted List
Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...
最新文章
- win7多国语的安装说明
- house price model
- 加油站(暴力+贪心)
- Git简单基本操作指令集合
- MQTT和HTTP的区别
- 多项新政催生本年度购房最佳“窗口期”
- 小动画制作 图片盒子配合定时器 winform 114869633
- 【impala】impala的shell命令使用
- WPF RichTextBox设置文本颜色
- 解决GBK字符转UTF-8乱码问题
- ADF12C UI根据row重定位VO的当前行
- 2016-06-07【普元EOS Studio】
- ​网红拉姆之死,这辈子最看不起这种男人!
- 故障树FTA(fault tree analysis)工具链
- alook浏览器_Alook浏览器app下载|Alook浏览器安卓版下载 v3.4.1 - 跑跑车安卓网
- 计算机毕业设计ssm民宿预定管理系统20sxh系统+程序+源码+lw+远程部署
- hypermesh和lsdyna联合仿真计算某汽车座椅进行的头冲吸能实验
- Gephi快速入门(一):Windows下安装Gephi
- 大学计算机实验六实验报告,大学计算机实验6 实验报告.pdf
- 火车头采集器V10下载-火车头采集器免费