Remove Duplicates from Sorted List II
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.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
分析: 为了便于管理头指针,创建一个新的头指针,利用几个新的变量来判断cur和prev的关系
/**
* 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)
return head; ListNode* start = new ListNode();
start->next = head; ListNode* cur = head;
ListNode* prev = start;
while(cur){
while(cur->next && cur->next->val == prev->next->val)
cur = cur->next;
if(prev->next == cur)
prev = cur;
else
prev->next = cur->next;
cur = cur->next;
}
return start->next;
}
};
Remove Duplicates from Sorted List II的更多相关文章
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- 48. Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that each ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- LeetCode之“链表”:Remove Duplicates from Sorted List && Remove Duplicates from Sorted List II
1. Remove Duplicates from Sorted List 题目链接 题目要求: Given a sorted linked list, delete all duplicates s ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- Leetcode: Remove Duplicates from Sorted List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- 一个过滤特殊字符的JS
<script language="javascript"> function checkForms() { var iu, iuu, regArray=new Arr ...
- JMeter专题系列(六)集合点
JMeter也有像LR中的集合点: JMeter里面的集合点是通过添加定时器来完成. 注意:集合点的位置一定要在Sample之前. 集合点:虽然我们的“性能测试”理解为“多用户并发测试”,但客观上来说 ...
- java多线程-锁
自 Java 5 开始,java.util.concurrent.locks 包中包含了一些锁的实现,因此你不用去实现自己的锁了.但是你仍然需要去了解怎样使用这些锁. 一个简单的锁 让我们从 java ...
- Boostrap全局CSS样式
1.Bootstrap提供的CSS Reset * { box-sizing: border-box; } body { font ...; color: #333; background: ...; ...
- IIS6.0添加上.net4.0后,以前的.net系统出现“服务器应用程序不可用”的错误提示解决办法
把VS2010开发的网站.net4.0部署到Windows Server 2003的服务器上去, Windows Server 2003操作系统自带的为IIS 6.0,IIS 6.0一般只支持.NET ...
- UITextFieldDelegate协议
很多人都认为UITextField很简单,为什么会写这个协议呢? 因为在实际开发中可能会用到: 比如要做到下图的效果: 文本框下面的下划线的颜色要随着输入的状态变化: 点击对应的文本框,其下面的线条变 ...
- SharePoint 2013 安装中间出错了怎么办? 每一次安装都是一段曲折的路【1603(0x643) 】
今天安装SharePoint 2013又出现了如下的错误,所有的必备软件都已经安装成功的情况下: 如何解决这样的问题呢? 1.首先把安装的日志文件找出来:位于 C:\用户\您的用户名\AppData\ ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q147-Q151)
Question 147 Your company has an existing SharePoint 2010 public-facing Web site. The Web site runs ...
- HashSet 浅析示例
* 1.继承自抽象类 AbstractSet,实现接口 Set.Cloneable.Serializable: * 2.元素无顺序: * 3.元素不可重复: * 4.采用哈希算法插入数据,插入速度快: ...
- 数据仓库之启用cdc
准备工作: 先将sqlservere 代理服务启动 USE [MyDB]; GO EXECUTE sys.sp_cdc_enable_db; --启用数据库对CDC的支持 GO -- 设置别名 @ca ...