【Lintcode】113.Remove Duplicates from Sorted List II
题目:
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
题解:
Solution 1 ()
- class Solution {
- public:
- ListNode *deleteDuplicates(ListNode *head) {
- if (head == nullptr) {
- return head;
- }
- ListNode* dummy = new ListNode(-);
- dummy->next = head;
- head = dummy;
- while (head->next != nullptr && head->next->next != nullptr) {
- if (head->next->val == head->next->next->val) {
- int value = head->next->val;
- while (head->next != nullptr && head->next->val == value) {
- head->next = head->next->next;
- }
- } else {
- head = head->next;
- }
- }
- return dummy->next;
- }
- };
二级指针
Solution 2 ()
- class Solution {
- public:
- ListNode *deleteDuplicates(ListNode *head) {
- if (head == nullptr || head->next == nullptr) {
- return head;
- }
- ListNode **t = &head;
- while (*t) {
- if ((*t)->next && (*t)->next->val == (*t)->val) {
- ListNode *tmp = *t;
- while (tmp && (*t)->val == tmp->val) {
- tmp = tmp->next;
- }
- *t = tmp;
- } else {
- t = &((*t)->next);
- }
- }
- return head;
- }
- };
【Lintcode】113.Remove Duplicates from Sorted List II的更多相关文章
- 【LeetCode】80. Remove Duplicates from Sorted Array II (2 solutions)
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 【Lintcode】112.Remove Duplicates from Sorted List
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. Examp ...
- 【LeetCode】80. Remove Duplicates from Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【原创】leetCodeOj ---Remove Duplicates from Sorted List II 解题报告
明日深圳行,心情紧张,写博文压压惊 囧 ------------------------------------- 原题地址: https://oj.leetcode.com/problems/rem ...
- 【Leetcode】82. Remove Duplicates from Sorted List II
Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only dis ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
随机推荐
- PHP实现上次登录功能
通过一个sql语句把上次的登录时间给本次登录时间,再把当前时间记录下来 update userinfo set lasttime=userinfo.logintime,logintime= CURR ...
- HDFS源码分析之DataXceiverServer
DataXceiverServer是Hadoop分布式文件系统HDFS的从节点--数据节点DataNode上的一个后台工作线程,它类似于一个小型的服务器,被用来接收数据读写请求,并为每个请求创建一个工 ...
- windows 10 python 2.7和python3.6共存解决方法和pip安装
一.首先去python官网将两个版本下载并安装: 然后进入windows的环境变量,检查下面4个变量: 1.C:\Python272.C:\Python27\Scripts3.D:\software\ ...
- 九度OJ 1021:统计字符 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5418 解决:3146 题目描述: 统计一个给定字符串中指定的字符出现的次数. 输入: 测试输入包含若干测试用例,每个测试用 ...
- 连通性 保证f具有介值性质
- Swift 学习笔记(面向协议编程)
在Swift中协议不仅可以定义方法和属性,而且协议是可以扩展的,最关键的是,在协议的扩展中可以添加一些方法的默认实现,就是在协议的方法中可以实现一些逻辑,由于这个特性,Swift是可以面向协议进行编程 ...
- 前端面试题(一)JS篇
内置类型 JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: null,undefined,boolean,number,string,symbo ...
- 【题解】P2048 [NOI2010]超级钢琴
[题解][P2048 NOI2010]超级钢琴 一道非常套路的题目.是堆的套路题. 考虑前缀和,我们要是确定了左端点,就只需要在右端区间查询最大的那个加进来就好了.\(sum_j-sum_{i-1} ...
- ajax实时获取下拉数据
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ajax ...
- Java基础教程:面向对象编程[2]
Java基础教程:面向对象编程[2] 内容大纲 访问修饰符 四种访问修饰符 Java中,可以使用访问控制符来保护对类.变量.方法和构造方法的访问.Java 支持 4 种不同的访问权限. default ...