【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 ...
随机推荐
- 图片压缩CompressUtil解析
CompressUtil 流程图: CompressUtil 类 具体解释 public class CompressUtil { /** * 终于封装的压缩方法 * @param imgPath * ...
- 深入理解Java 8 Stream
Java 8中新增了Stream,主要是lambda表达式的应用,其链式调用简洁,用于高效表达集合操作. 先对Stream的使用做了解,参照blog. (1) 生成Stream的方式 主要有以下几种 ...
- python学习(九)python中的变量、引用和对象的关系
<Think In Java>中说到过"万事万物皆对象",这句话也可以用在Python中. 感觉Python中的变量有点像Javascript中的变量,是弱类型的,但是 ...
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
// ConsoleApplication10.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream& ...
- golang截取字符串
对于字符串操作,截取字符串是一个常用的, 而当你需要截取字符串中的一部分时,可以使用像截取数组某部分那样来操作,示例代码如下: package main import "fmt" ...
- 查看SELinux状态并关闭SELinux
SELinux(Security-Enhanced Linux)是Linux上最杰出的新安全子系统.在linux内核级别上提供了一个灵活的强制访问控制系统(MAC),这个强制访问控制系统是建立在自由访 ...
- 再看GS接包过程
再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- nexus搭建maven私服及私服jar包上传和下载
nexus搭建maven私服及私服jar包上传和下载 标签: nexus管理maven库snapshot 2017-06-28 13:02 844人阅读 评论(0) 收藏 举报 分类: Maven(1 ...
- 将众多小文件输入Hadoop的解决方案 可挂载的HDFS
配置HDFS为可挂载后: 1-可挂载后才支持非完整POSIX语义: 2-仍然不支持随机写入,仍然为“一次写入,多次读取”: 3-可能误用,导致众多小文件: : 1-使用Solr存储和检索小文件: 2- ...