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 ...
随机推荐
- CSS背景图拉伸不变形
在线效果体验:http://hovertree.com/texiao/mobile/3.htm 请使用手机浏览器查看. css代码: .bg{ background:url(http://hovert ...
- javascript中BOM部分基础知识总结
一.什么是BOM BOM(Browser Object Document)即浏览器对象模型. BOM提供了独立于内容 而与浏览器窗口进行交互的对象: 由于BOM主要用于管 ...
- 简要分析webpack打包后代码
开门见山 1.打包单一模块 webpack.config.js module.exports = { entry:"./chunk1.js", output: { path: __ ...
- CSS字符编码引起乱码
乱码引起的CSS失效原理: 由于一个中文是两个字符组成,在编码不一致的情况下会引发字符的“重新”组合,(半个汉字的编码字符与后面的字符组合生成新的“文字”)引发原本的结束符合“变异”,从而导致 ...
- iOS 开源项目
在 Github 上 Star 太多了,有时候很难找到自己想要的开源库,所以在此记录下来.便于自己开发使用,也顺便分享给大家. 动画 awesome-ios-animation收集了iOS平台下比较主 ...
- 获取OpenFileDialog的文件名和文件路径
得到文件名 string fileName = ofd.SafeFileName; 得到路径 string filePath = System.IO.Path.GetDirectoryName(ofd ...
- 未能加载包“Microsoft SQL Server Data Tools”
直接在vs2013里的App_Data目录创建数据库,在服务器资源管理器中查看时报错: 未能加载包“Microsoft SQL Server Data Tools” 英文: The 'Microsof ...
- SharePoint Permission Extension
SharePoint Permission Extension 项目很久没维护了,也没有迁移到sp2013上(貌似只要把2013的Form的RenderMode设置为Server后也是可以用的). 在 ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- 常用API——字符串String型函数
上图: 声明 var myString = new String(“Every good boy does fine.”); var myString = “Every good boy does f ...