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.

题意:给定一个已排序的链表,删除其中重复的元素,保证每个元素有且仅有一个。

使用两个指针,分别指向前一个元素,和当前元素,比较两者。如果相同,则删除当前元素;否则,将当前元素换成新值。

public:
ListNode *deleteDuplicates(ListNode *head) {
if(head==NULL || head->next==NULL) return head;
ListNode *pre,*tail;
pre=head;
tail=head->next;
while(tail){
if(tail->val == pre->val){
pre->next=tail->next;
}else{
pre =pre->next;
}
tail=tail->next;
}
return head;
}
};

转载请注明出处:http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Remove Duplicates from Sorted List的更多相关文章

  1. leetcode题解: Remove Duplicates from Sorted List(已排序单链表去重)

    题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...

  2. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  3. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  4. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

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

  6. [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

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

  8. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

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

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

随机推荐

  1. Android 4 学习(20):ActionBar

    参考<Pro Android 4.0> ActionBar 11.0之后,ActionBar在Activity中默认存在,可以在代码中设置其显示与否: ActionBar actionBa ...

  2. openLayers 3 之入门

    openLayers 3 之入门 openlayer是web GIS客户端开发提供的javascript类库,也是开源框架,可以加载本地数据进行展示地图 1.下载相关引用的js.css文件 2.类似于 ...

  3. IIS Manager 配置文件修该,允许跨域CORS访问

    IIS Manager 配置文件修该,允许跨域CORS访问 IIS Manager 的api访问会出现跨域问题,需要 IIS Manager的配置文件中修改. 配置文件的路径:C:\Program F ...

  4. Shiro的Subject和Sessoin的创建

    之前要先了解Session的来源Shiro session和Spring session一样吗? 创建Subject的位置 AbstractShiroFilter . doFilterInternal ...

  5. Shiro的校验Session是否过期处理的过程

    首先开启定时扫描活跃的session进行校验 <!-- shiro会话管理 --> <!-- 即用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中:会话可以是普通 Jav ...

  6. C语言清空输入缓冲区的N种方法对比(转)

    C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); //获取行系列 ...

  7. Qt(自适应窗口)

    关于窗口布局: 默认控件的大小为最小尺寸: 如果此时右键设置布局时,窗口大小会自动缩放对应大小,不利于调整. 建议窗口控件设置好最小尺寸,便于窗口布局时,控件不会变形,例如: 改变minimumsiz ...

  8. 【307】◀▶ Python 相关功能实现

    目录: 1. Python 实现下载文件 2. 删除文件名中的点 “.” 3. 让 Python 脚本暂停执行的方法 4. 添 1. Python 实现下载文件 使用 urllib 模块提供的 url ...

  9. ubuntu kylin 设置 wifi

    左上侧  搜索资源 输入 软件和更新  点击软件和更新,点击附加驱动,点击使用无线驱动.

  10. mysqldump test

    CREATE TABLE IF NOT EXISTS `runoob_tbl`( `runoob_id` INT UNSIGNED AUTO_INCREMENT, `runoob_title` VAR ...