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. mysql 随机查询 记录集

    有时候需求需要随机从数据库查询若干条记录集,网上搜了一下,几篇博文都是些重复的.....不知道他们谁抄的谁的,这里除了介绍提供一种笔者自己想到的方法,本质都是利用mysql 的rand() 第一种方法 ...

  2. ARP跨网段广播

    如果源主机和目的主机不在同一网段,ARP请求的广播帧无法穿过路由器,源主机如何与目标主机实现通信? 补充一下问题,避免因问题不完整而出现误解. ---------------------------- ...

  3. unity2017.1.0f3与旧的粒子系统不兼容

    在测试旧版本插件unistorm时用unity2017.1.0f3打开后其它天气效果显示正常,雨点看不到,再用unity5.52打开后,所有效果都可以看到了. 记录备忘

  4. 自动删除 Elasticsearch 索引

    #!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...

  5. iOS学习之UIDatePicker控件使用

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. ,   您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Ti ...

  6. spring注解扫描组件注册

    最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...

  7. MongoDB常用增删改查语句

    数据库database 创建及查看库 1.有则使用这个数据库,没有就创建 use DATABASE_NAME 2. 查看当前选择的数据库,默认是test db 3.查看数据库,默认有admin.loc ...

  8. Spark会产生shuffle的算子

    去重 def distinct() def distinct(numPartitions: Int) 聚合 def reduceByKey(func: (V, V) => V, numParti ...

  9. Opencv 直方图比较

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  10. SpringBoot与Shiro的整合(单体式项目)

    1.包结构 2.jar包,配置文件,类 2.1pom.xml文件配置 <?xml version="1.0" encoding="UTF-8"?> ...