Total Accepted: 90247 Total Submissions: 254602 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.

/**
* 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的更多相关文章

  1. [Linked List]Remove Duplicates from Sorted List II

    Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium Given a sorted linked list, delet ...

  2. 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 ...

  3. 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题要求 ...

  4. [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 ...

  5. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  6. [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

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

  7. Leetcode-83 Remove Duplicates from Sorted List

    #83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...

  8. Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  9. 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 ...

随机推荐

  1. Android中webView的基础使用(一)

    WebView是View的一个子类,可以让你在activity中显示网页. 可以在布局文件中写入WebView:比如下面这个写了一个填满整个屏幕的WebView: <?xml version=& ...

  2. NodeJS初学者实战之旅(I) —— 介绍、目录

    旅行之初 作为一个工作了两年多后端开发人员,未接触过NodeJS,对它的认知也仅仅停留在“可以使用JavaScript代码编写服务端程序”上.最近空闲时间 较多,便想来啃它一啃.但是没有一个实际的玩意 ...

  3. JSP基础之 C标签中的 varStatues属性

    变量状态参数,该属性有5个常用值count 表示当前遍历集合的元素个数index 表示当前遍历到集合的第几个元素current 表示当前的集合元素first 表示集合的第一个元素last 表示集合的最 ...

  4. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  5. Java三大特征之封装(一)

    封装从字面上来理解就是包装的意思,专业点就是信息隐藏,是指利用抽象数据类型将数据和基于数据的操作封装在一起,使其构成一个不可分割的独立实体,数据被保护在抽象数据类型的内部,尽可能地隐藏内部的细节,只保 ...

  6. MySql优化方案

    mysql优化方案总结 u       Mysql数据库的优化技术 对mysql优化时一个综合性的技术,主要包括 a: 表的设计合理化(符合3NF) b: 添加适当索引(index) [四种: 普通索 ...

  7. MySql小知识点

    1.查看MySql是什么编码 show create table tablename;

  8. MySQL高效获取记录总数

    通常mysql获取查询记录总数我们使用如下语句: SELECT COUNT(*) FROM users WHERE k='avs';  或:SELECT id FROM goods WHERE k=' ...

  9. 【转】aiohttp 源码解析之 request 的处理过程

    [转自 太阳尚远的博客:http://blog.yeqianfeng.me/2016/04/01/python-yield-expression/] 使用过 python 的 aiohttp 第三方库 ...

  10. 关于ajax发送的数据问题

    今天在做保存富文本编辑器的内容时,发送了一个ajax请求: $.ajax({ type:"POST", url:"{% url 'cms:add' %}", d ...