【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错。
第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过。
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;
}
}
/**
* 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的更多相关文章
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【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 ...
- 【数组】Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【Leetcode】【Medium】Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【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 ...
- 【leetcode】Remove Duplicates from Sorted List
题目简述 Given a sorted linked list, delete all duplicates such that each element appear only once. For ...
- 【leetcode】 Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
随机推荐
- python字典访问的三种方法
定义字典 dic = {'a':"hello",'b':"how"} 法一: for key in dic: print key,dic[key] # a he ...
- Jmeter组件4. Regular Expression Extractor
位置:Post-Processors - Regular Expression Extractor 所谓的Post-Processors直译为后处理器,意思是在域内所有Sampler执行完后才会执行, ...
- 常用的SQL分页
有关分页 SQL 的资料很多,有的使用存储过程,有的使用游标.本人不喜欢使用游标,我觉得它耗资.效率低:使用存储过程是个不错的选择,因为存储过程是经过预编译的,执行效率高,也更灵活.先看看单条 SQL ...
- 获取body标签元素方法
方法一 doucumnet.body 方法二 document.getElementsByTagName("body")[0]
- C++中关于string类型究竟能不能用cout输出的问题
先让我讲下故事哈 一次在MFC中用cout输出一个string类型字符串,编译时出现这样一个错误: error C2679: binary '<<' : no operator defin ...
- 正则验证:Pattern,Matcher
public static void main(String[] args) { String regex="([a-z]{1})(\\d{2})"; String candida ...
- Autofac 的属性注入方式
介绍 该篇文章通过一个简单的 ASP.NET MVC 项目进行介绍如何使用 autofac 及 autofac 的 MVC 模块进行依赖注入.注入方式通过构造函数.在编写 aufofac 的依赖注入代 ...
- HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
根据奇偶开两个hash表来记录后缀和.注意set会被卡,要手写hash表. 具体见代码: #include <stdio.h> #include <algorithm> #in ...
- Couldn't resolve Mac Server "mymac"
vs2015创建一个iphone app ,Couldn't resolve Mac Server “mymac” 伤.下班走人
- Rule of Modularity
As Brian Kernighan once observed, “Controlling complexity is the essence of computer programming.” . ...