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. maven引入源码

    选中要添加的源码的项目右键-->debug--->debugs-configurations-->source-->java project

  2. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 14—Dimensionality Reduction 降维

    Lecture 14 Dimensionality Reduction 降维 14.1 降维的动机一:数据压缩 Data Compression 现在讨论第二种无监督学习问题:降维. 降维的一个作用是 ...

  3. 如何在windows下安装mongoDB扩展

    安装环境   系统环境:Windows 10 64位   Apache版本:2.4.9   PHP版本:5.5.12   MongoDB版本:3.2.6   Wamp版本:wamp 2.5 86位   ...

  4. Scala开发Hadoop示例

    import org.apache.hadoop.conf.{Configuration, Configured}; import org.apache.hadoop.util.{ToolRunner ...

  5. Eclipse使用时遇到的问题

    Java更新之后,Eclipse运行程序时提示 无法找到Java可执行文件 字符串之间判断用.equals,不能用== String a; String b; if(a.equals(b)){...} ...

  6. java.lang.NoSuchMethodError: org.springframework.dao.IncorrectResultSizeDataAccessException

    spring data jpa  运用,在dao类中写自己新增的方法,使用@query写hql语句,出现以下异常: Caused by: java.lang.NoSuchMethodError: or ...

  7. freeswitch由于ext-sip-ip地址填写错误导致32秒拆线问题

    通话32秒左右就断掉 检查 profile 的 ext-sip-ip 设置ext-rtp-ip和ext-sip-ip 可以直接设置为外网IP 自建stun-server, 更新后, 过了好几个小时出现 ...

  8. Uniform & Attribute & Varying

    [Uniform & Attribute & Varying] 顶点着色器的输入变量用关键字“attribute”来限定. 片段着色器的输入变量(它和顶点着色器的输出变量相对应)用关键 ...

  9. windows下python安装Numpy和Scipy模块

    安装 numpy: 去 http://sourceforge.net/projects/numpy/files/latest/download?source=files 下载相应的exe安装文件. 安 ...

  10. spring+springmvc+mybatis+redis实现缓存

    先搭建好redis环境 需要的jar如下: jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:330 ...