【Lintcode】112.Remove Duplicates from Sorted List
题目:
Given a sorted linked list, delete all duplicates such that each element appear only once.
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
题解:
Solution 1 ()
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
ListNode* cur = head;
while (cur != nullptr && cur->next != nullptr) {
if (cur->val == cur->next->val) {
cur->next = cur->next->next;
continue;
}
cur = cur->next;
}
return head;
}
};
Solution 2 ()
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head || !head->next) return head; ListNode *front, *last; front = head;
last = head->next; while(last)
{
if(front->val == last->val)
{
front->next = last->next;
delete last;
last = front->next;
}
else
{
front = last;
last = last->next;
}
} return head; }
};
【Lintcode】112.Remove Duplicates from Sorted List的更多相关文章
- 【Lintcode】113.Remove Duplicates from Sorted List II
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- 【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 ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【LeetCode】83 - Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- 【LeetCode】83 - Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode】026. Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
随机推荐
- 邮箱大师WPZ协议包
WIRELESS Z PACKET: i8-version(WZPUnit.getVersion() & 3 | WZPUnit.MAGIC_MASK = 1 & 3 | -48 = ...
- iostat命令具体解释——linux性能分析
之前总结uptime和free命令,今天继续来总结一下iostat.给自己留个笔记.同一时候也希望对大家实用. 版本号信息: sysstat version 9.0.4 (C) S ...
- Dell 刀片服务器CentOS6.5mini开机20~30分钟宕机
今天查看系统日志发现大量的nf_conntrack: table full, dropping packet. 错误 cat /var/log/messages | moreJun 7 09:52: ...
- Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了
前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ...
- PHP合并数组+与array_merge的区别
http://www.phpernote.com/php-string/351.html PHP中合并两个数组可以使用+或者array_merge,但这两个还是有区别的 主要区别是当两个或者多个数 ...
- [nio]dawn的基本概念
1.dawn是单线程的: 为什么单线程?现实中非常多程序都是单线程的.比方redis,memcache,nodejs.mmorpgserver..... . 採用单线程有两大优点,首先,不须要使用锁, ...
- Python 单元测试 之setUP() 和 tearDown()
setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...
- 九度OJ 1040:Prime Number(质数) (递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5278 解决:2180 题目描述: Output the k-th prime number. 输入: k≤10000 输出: The k- ...
- BZOJ 3037 创世纪
题解: 首先从基环树上的环上选两个点x,y 断开x,y之间的边,然后做树形DP. 设f[x]为选x的情况下的最大值,g[x]为不选x的情况下的最大值. 分两种情况讨论, 1.选x,则y一开始就处于被支 ...
- 20145239 实验一 Java开发环境的熟悉(Windows + IDEA)
实验一 Java开发环境的熟悉(Windows + IDEA) 实验内容 1.使用JDK编译.运行简单的Java程序:2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验知识点 1.JV ...