LeetCode[Linked List]: 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, return
1->2->3->3->4->4->5.
1->2->5
Given, return
1->1->1->2->3.
2->3
非经常规的解法。为去除头节点的特殊性,须要用到虚拟头结点技术。
ListNode *deleteDuplicates(ListNode *head) {
if (!head) return NULL;
ListNode *dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode *prev = dummyHead, *curr = head->next;
int curVal = head->val;
while (curr) {
if (curr->val == curVal) {
for (curr = curr->next; curr != NULL && curr->val == curVal; curr = curr->next) ;
prev->next = curr;
if (curr) {
curVal = curr->val;
curr = curr->next;
}
}
else {
prev = prev->next;
curVal = curr->val;
curr = curr->next;
}
}
return dummyHead->next;
}
LeetCode[Linked List]: Remove Duplicates from Sorted List II的更多相关文章
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- 【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】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- LeetCode OJ 82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【LeetCode】080. Remove Duplicates from Sorted Array II
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
- leetcode笔记:Remove Duplicates from Sorted Array II
一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...
随机推荐
- JS base64加解密解决传输的url各种编码问题
网上拷贝的,废话少说,直接上代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 【转】跟我一起学Spring 3(4)–深入理解IoC(控制反转)和DI(依赖注入)
在继续下面的章节之前,我们要先说说大名鼎鼎的IoC和DI. 我们经常会听说IoC,也就是Inversion of Controller,控制反转.事实上,IoC并不是一个新鲜的概念,最早可能是在198 ...
- Web Storage与Cookie相比存在的优势:
(1).存储空间更大:IE8下每个独立的存储空间为10M,其他浏览器实现略有不同,但都比Cookie要大很多. (2).存储内容不会发送到服务器:当设置了Cookie后,Cookie的内容会随着请求一 ...
- xampp 修改 mysql 默认 root 密码
xampp 默认的 mysql 的 root 密码是空的,种种原因想给它加上. 最有效简单方法是使用 phpmyadmin. 初始状态下,我们可以使用 http://localhost/phpmyad ...
- 记:青岛理工ACM交流赛筹备工作总结篇
这几天筹备青岛理工ACM交流赛的过程中遇到了不少问题也涨了不少经验.对非常多事也有了和曾经不一样的看法, 一直在想事后把这几天的流水帐记一遍,一直没空直到今天考完C++才坐下来開始动笔.将这几天的忙 ...
- JQuery EasyUI学习笔记
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6690888.html 简介与准备 jQuery EasyUI 是一个基于 jQuery 的框架,集成了各种用 ...
- 〖Linux〗穿越城墙之后,直接连接国内网站的路由配置
因为有需要做Android相关的开发工作,很多时候要穿越之后才能做事情: 如Android文件加密预研.Android NDK/SDK的下载,都需要使用得到Google: 但是穿越之后,访问国内网站就 ...
- Redis从入门到精通:中级篇(转)
原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...
- Arduino通过I2C(SSD1306)驱动0.96寸12864OLED
I2C驱动的128x64 OLED I2C (Inter-Integrated Circuit) 集成电路总线是I2CBus的简称, 是一种串行通信总线, 使用多主从架构. 飞利浦公司在1980年代为 ...
- Arduino和C51开发光敏传感器
技术:51单片机.Arduino.光敏传感器.PCF8591.AD/DA转换 概述 本文介绍了如何接收传感器的模拟信号和如何使用PCF8591 AD/DA转换模块对光敏传感器的模拟信号进行转换.讲 ...