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.

题目要求:

给一有序链表,删除重复的结点,使得每个元素只出现一次。

解题思路:

1、从头到尾遍历链表,如果前后两个结点相同,则将第一个结点指向它的下下结点,并删除它的下个结点。

2、递归思想。

代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *deleteDuplicates(ListNode *head) {ListNode *lst=head;ListNode *del;while(lst && lst->next){if(lst->val==lst->next->val){del=lst->next;lst->next=lst->next->next;delete(del);}elselst=lst->next;}return head;}
};

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode *deleteDuplicates(ListNode *head) {if(head==NULL || head->next==NULL)return head;if(head->val==head->next->val)head=deleteDuplicates(head->next);elsehead->next=deleteDuplicates(head->next);return head;}
};

转载于:https://www.cnblogs.com/AndyJee/p/4464233.html

(LeetCode 83)Remove Duplicates from Sorted Lists相关推荐

  1. 删除重复值(2以上)Remove Duplicates from Sorted Array II

    2019独角兽企业重金招聘Python工程师标准>>> 问题: Follow up for "Remove Duplicates": What if duplic ...

  2. LeetCode算法入门- Remove Duplicates from Sorted Array -day21

    LeetCode算法入门- Remove Duplicates from Sorted Array -day21 题目描述 Given a sorted array nums, remove the ...

  3. LeetCode刷题(48)--Remove Duplicates from Sorted List II

    cur表示当前所在的Node,对于重复出现的Node会移到最后一个. pre记录结果,res用于返回,pre的更改会体现在res上. 如果pre.next == cur,则说明没有重复,此时pre = ...

  4. LeetCode刷题(45)--Remove Duplicates from Sorted List

    一个遍历列表,一个记录结果,一个记录head! class Solution(object):def deleteDuplicates(self, head):""":t ...

  5. LeetCode刷题(44)--Remove Duplicates from Sorted Array II

    允许重复两次,关注结果,快的n扫描列表,慢的i记录位置. class Solution(object):def removeDuplicates(self, nums):""&qu ...

  6. 【算法】LeetCode算法题-Remove Duplicates from Sorted Array

    这是悦乐书的第149次更新,第151篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第8题(顺位题号是26).给定一个已经排序(由小到大)的整数数组(元素可以重复),计算其 ...

  7. 【LeetCode OJ】Remove Duplicates from Sorted List

    2019独角兽企业重金招聘Python工程师标准>>> Given a sorted linked list, delete all duplicates such that eac ...

  8. (LeetCode 203)Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  9. leetcode(83)—— 删除已排序链表重复元素(Remove Duplicates from Sorted List)

    Remove Duplicates from Sorted List 官方答案(Java): Remove Duplicates from Sorted List 思路:维护两指针(ListNode ...

最新文章

  1. php libev pthreads,libuv 与 libev 的对比
  2. java在初始化过程_Java初始化过程
  3. a 寻路算法 java_A*(也叫A star, A星)寻路算法Java版 | 学步园
  4. linux的QQ邮件告警,QQ邮箱告警注意点
  5. (转)SpringMVC学习(十一)——SpringMVC实现Resultful服务
  6. 用VBScript实现Zip压缩目录中的所有文件
  7. 体温监测行业调研报告 - 市场现状分析与发展前景预测
  8. 关于信道利用率的总结与一道习题的最终解释
  9. canoe开发从入门到精通_后端java开发工程师学习路线
  10. 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果...
  11. 535. Encode and Decode TinyURL - LeetCode
  12. 212.单词搜索II
  13. EditPlus3.3 集成 SVN
  14. 信息学奥赛之数学一本通 pdf_整理青少年信息奥赛
  15. Fujitsu Lifebook U1010安装XP TabletPC 2005完全攻略
  16. 微会动微营销引擎:SEM效果提升的2大方向+5种能力+7个策略
  17. 移动开发技术【安卓】——Android_Studio【Part 1】
  18. 计算机图像处理2000字论文,图像处理计算机技术论文
  19. 为什么阿里那么难进,原来精髓在这
  20. 以太坊bloom和logs及代码解析

热门文章

  1. Linux下文件的三个时间意义及用法
  2. HDU 5514 Frogs (容斥原理+因子分解)
  3. linux中send函数MSG_NOSIGNAL异常消息
  4. 解决Eclipse无法打开“Failed to load the JNI shared library”(转)
  5. Fckeditor配置 for ASP.NET
  6. 致:WWF技术博客领跑者WXWINTER--兰竹梅菊.春夏秋冬
  7. 洛谷 2585 [ZJOI2006]三色二叉树——树形dp
  8. Matlab实现二进制矩阵转换为十进制
  9. awk分析nginx日志里面的接口响应时间
  10. C/C++中extern关键字详解[zz]