【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 ...
随机推荐
- Netty(三):线程模型
Netty中支持单线程模型,多线程模型,主从多线程模型. 1 单线程模型 在ServerBootstrap调用方法group的时候,传递的参数是同一个线程组,且在构造线程组的时候,构造参数为1,这种开 ...
- caffe2--------ImportError: No module named past.builtins
whale@sea:~/anaconda2/lib/python2.7/site-packages$ python Python 2.7.14 |Anaconda custom (64-bit)| ( ...
- HTML5之Canvas绘图(二) ——应用篇之七巧板
1.canvas绘制七巧板-- <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- mysql 查排名
SET @amount=0; SET @rank=1; SET @shunxu=0; SELECT tmp2.id AS id,tmp2.name AS NAME,tmp2.amount AS ...
- checkStyle使用具体解释
简单介绍 checkStyle是一款代码格式检查工具.它依据设置好的编码规则来自己主动检查代码.比方命名规范,文件长度.代码行长度等等.代码检查工具是保证项目代码质量.统一编码风格的一种重要途径.本篇 ...
- 高速修复汉澳sinox命令解释程序bash shell漏洞
bash是linux默认命令行管理程序shell.汉澳 sinox也安装有,尽管sinox并没有默认使用bash.可是用户一旦使用就会可能被通过漏洞入侵,所以必须高速修复.尽管sinox使用freeb ...
- Android中RelativeLayout各个属性及其含义
android:layout_above="@id/xxx" --将控件置于给定ID控件之上android:layout_below="@id/xxx" - ...
- python 基础 5.3 类的重写
一. 类的重写 只需要重新定义类的属性(变量),就是累的重写了 示例:重新定义类grandson的 name属性 #/usr/bin/python #coding=utf-8 #@Time :20 ...
- 【BZOJ3696】化合物 树形DP+暴力
[BZOJ3696]化合物 Description 首长NOI惨跪,于是去念文化课了.现在,他面对一道化学题. 这题的来源是因为在一个奇怪的学校两个化竞党在玩一个奇怪的博弈论游戏.这个游戏很蛋疼 ...
- java常量池概念 (转)
在class文件中,“常量池”是最复杂也最值得关注的内容. Java是一种动态连接的语言,常量池的作用非常重要,常量池中除了包含代码中所定义的各种基本类型(如int.long等等)和对象型(如Stri ...