[Linked List]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
.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* pre=NULL,*p=head,*next=NULL;
while(p){
if(pre && pre->val == p->val){
pre->next = p->next;
delete(p);
p = pre->next;
}else{
pre = p;
p = p->next;
}
}
return head;
}
};
[Linked List]Remove Duplicates from Sorted List的更多相关文章
- [Linked List]Remove Duplicates from Sorted List II
Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium Given a sorted linked list, delet ...
- LeetCode[Linked List]: Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- Leetcode-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
随机推荐
- C#关于params的用法(使用数量可变的参数)
有些方法需要传递个数不定的值进行运算.比如求最小值的方法.除了用容器外,还可以使用params来做 例子如下: using System; using System.Collections.Gener ...
- 简单的oracle sql 语句
创建表空间 create tablespace qnhouse --表空间文件路径 datafile 'E:\qnhost\qnhouse.dbf' --表空间文件大小 size 100M; 创建用户 ...
- Foundation--NSDictionary+NSMutableDictionary
键值对 key(一般为字符串对象)---vaule(必须是对象) Person *p1 =[[Person alloc ]init]; Person *p2 =[[Person alloc ]init ...
- 【IOS学习基础】文件相关
一.沙盒(SandBox) 1.沙盒机制 1> 每个应用都有属于自己的存储空间,即沙盒. 2> 应用只能访问自己的沙盒,不可访问其他区域. 3> 如果应用需要进行文件操作,则必须将文 ...
- UTF-8、GB2312都支持的汉字截取函数
<?php/*Utf-8.gb2312都支持的汉字截取函数cut_str(字符串, 截取长度, 开始长度, 编码);编码默认为 utf-8开始长度默认为 0*/ function cut_str ...
- ~/microwindows-0.89pre8/src/bin$ ./nano-X error:Cannot bind to named socket
GUI:microwindows-0.89pre8+nona-X you are successful compiling, run nano-X,below is information: ~/mi ...
- python成长之路第二篇(4)_collections系列
一.分别取出大于66的数字和小于66的数字 小练习:需求要求有一个列表列表中存着一组数字,要求将大于66的数字和小于66的数字分别取出来 aa = [11,22,33,44,55,66,77,88,9 ...
- MYSQL while 、repeat
前期准备: createt table employee(ID int ,Name varchar(4)); 用下列方法向表中循环插入数据. ----------------------------- ...
- Jmeter性能测试 及压测入门
Jmeter是一个非常好用的压力测试工具. Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 为什么要建立线程组?原因很简单,因为我们要模拟多个线程(用户 ...
- haproxy redirect prefix
acl short_domain hdr(Host) -i etiantian.org redirect prefix http://www.etiantian.org code 301 if sho ...