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. krpano之缩略图文本添加

    效果: 在缩略图上添加文本,显示缩略图名称. 方法:将皮肤中的 skin_addthumbs 方法替换为一下代码. <action name="skin_addthumbs" ...

  2. django -- 自定义simpletag 和 filter

    django中自定义simpletag,即使用自己定义的函数在html中处理数据. 步骤: 1.创建并注册app settings.py INSTALLED_APPS = [ 'django.cont ...

  3. C++Primer笔记-----day04

    1.函数指针.函数指针指向某种特定类型,函数的类型由它的返回类型和形参类型决定,与函数名无关.比如:bool lengthCompare(const string &,const string ...

  4. ubuntu 初始设置备忘

    配置静态网络 vim /etc/network/interfaces auto eth0 #iface eth0 inet dhcp iface eth0 inet static address x. ...

  5. win7下IIS的安装和配置图文教程

    1. 首先是安装IIS.打开控制面板,找到”程序与功能”,点进去 2. 点击左侧”打开或关闭Windows功能” 3. 找到”Internet 信息服务”,按照下图打勾即可 等待安装完成 4. 安装完 ...

  6. go cobra

    https://github.com/spf13/cobra https://github.com/spf13/cobra/blob/master/bash_completions.md go get ...

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

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

  8. html学习代码

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  9. Spark internal - 多样化的运行模式(上)

    Spark的运行模式多种多样,在单机上既可以以本地模式运行,也可以以伪分布式模式运行.而当以分布式的方式运行在Cluster集群中时,底层的资源调度可以使用Mesos 或者是Hadoop Yarn , ...

  10. 卸载 Windows 8/8.1/10 无法常规卸载的内置应用

    现在已经有一款可以卸载内置应用的软件了:http://www.thewindowsclub.com/10appsmanager-windows-10 在应用商店里下了一个计算器+,于是想把内置的计算器 ...