给定一个排序链表,删除所有重复的元素使得每个元素只留下一个。
案例:
给定 1->1->2,返回 1->2
给定 1->1->2->3->3,返回 1->2->3
详见:https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/

Java实现:

递归实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head==null||head!=null&&head.next==null){
return head;
}
ListNode tmp=head;
ListNode next=deleteDuplicates(head.next);
if(next!=null){
if(tmp.val==next.val){
tmp.next=next.next;
}
}
return head;
}
}

非递归实现:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur=head;
while(cur!=null){
while(cur.next!=null&&cur.val==cur.next.val){
cur.next=cur.next.next;
}
cur=cur.next;
}
return head;
}
}

083 Remove Duplicates from Sorted List 有序链表中删除重复的结点的更多相关文章

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

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  2. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  3. LeetCode 83. Remove Duplicates from Sorted List(从有序链表中删除重复节点)

    题意:从有序链表中删除重复节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...

  4. LeetCode 82,考察你的基本功,在有序链表中删除重复元素II

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...

  5. [LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)

    描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  6. Remove Duplicates from Sorted List ,除去链表中相邻的重复元素

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

  7. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

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

  8. Remove Duplicates from Sorted List 去除链表中重复值节点

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

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

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

随机推荐

  1. 'gbk' codec can't encode character '\xa5' in position 4546: illegal multibyte sequence错误解决

    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030') 原文 http://blog.csdn.net/jim7424 ...

  2. gzhu 2013 Good Sequence 解题报告

    题目链接:(这个是内网的网址)  http://172.22.27.1/problem?pid=1013 Good Sequence Time Limit: 4000/2000 MS (Java/Ot ...

  3. android中requestFocus

    标签用于指定屏幕内的焦点View. 例如我们点击tab键或enter键焦点自动进入下一个输入框用法: 将标签置于Views标签内部 <EditText id="@+id/text&qu ...

  4. Yii的缓存机制之片段缓存

      一.首先在main.php配置缓存组件 在components里面添加cache项.代码如下: // application components 'components'=>array( ...

  5. JDBC 笔记3 通过PreparedStatement 对数据库进行增删改查 (转载)

    之前MVC时一直用它,学了框架后就没怎么用了.这里转载一位同学的博客,以后可能也会用到的. 转自:https://www.cnblogs.com/zilong882008/archive/2011/1 ...

  6. Activity参数传递

    简单数据传递 putExtra() 传递 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Over ...

  7. AngularJS系统学习之$watch(监控)

    在scope的内置的所有函数中,用的最多的可能就是$watch函数了, 当你的数据模型中某一部分发生变化时,$watch函数可以向你发出通知. 你可以监控单个对象的属性,亦可以监控需要经过计算的结果( ...

  8. JAVA通过信号量避免死锁

    死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 导致死锁的根源在于不适当地运用"synchronized ...

  9. 2.1-2.2 HBase数据存储

    一.HBase数据检索流程 一篇介绍HBase数据读写流程的解析的博文:http://hbasefly.com/2016/12/21/hbase-getorscan/?wsfatm=uqvhl3 1. ...

  10. 发送邮件小工具(python)

    #!/usr/bin/python # -*- coding:UTF- -*- import sys import smtplib import email.mime.multipart import ...