[LeetCode] 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.
Before I get started, I should write down what I know for now, its a singly-linked-list related problem, and its already been sorted, that mean every duplicate numbers is next to each other. note that it requires to remove all the duplicates, seems like an easy job.
ListNode *deleteDuplicates(ListNode *head) {
ListNode* current, *prev;
ListNode base = ListNode(-10);
base.next = head;
current = &base; prev = NULL;
while (current && current->next){
if (current->val == current->next->val){
while (current->next && current->val == current->next->val){
current = current->next;
}
prev->next = current->next;
current = current->next; }else {
prev = current;
current = current->next;
} }
return base.next;
}
By inserting a dummy node with some invalid number(I choose -1 at first, but a test case uses -1 as 1st val as well...wtf) before head is a good trick, it'll allow you to delete head of current list. anyway, operating linked list in c++ is much convenient then c. In c, you can't change list structure by not involving any double pointer.
[LeetCode] 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] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [Leetcode] Remove Duplicates From Sorted Array II (C++)
题目: 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 exampl ...
- [LeetCode] Remove Duplicates from Sorted Array II [27]
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- [leetcode]Remove Duplicates from Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- LeetCode::Remove Duplicates from Sorted List II [具体分析]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
随机推荐
- ubuntu配置apache的虚拟主机
ubuntu中apache的配置文件分散在几个文件中,/etc/apache2/apache2.conf将它们组织起来.这样设计有很多好处,这里就不在赘述了.进入正题: 1)配置文件在/etc/apa ...
- 转:理解Cookie和Session机制
原文: 理解Cookie和Session机制 摘要: Cookie工作原理 由于HTTP是一种无状态的协议,服务器单从网络连接上无从知道客户身份.怎么办呢?就给客户端们颁发一个通行证吧,每人一个,无论 ...
- MongoDB-Getting Started with the C# Driver
简介:本文仅提供快速入门级别的使用C# Driver操作MongoDB,高手跳过 Downloading the C# Driver 猛击下载 添加相关的dll引用 MongoDB.Bson.dll ...
- Shell 读取文本内容
在Linux中有很多方法逐行读取一个文件的方法,其中最常用的就是下面的脚本里的方法,而且是效率最高,使用最多的方法.为了给大家一个直观的感受,我们将通过生成一个大的文件的方式来检验各种方法的执行效率. ...
- C++实现VPN工具之VPN错误代码大全
该篇文章转自:<VPN问题全攻略>http://home.51.com/h012359/diary/item/10008457.html 以下是使用VPN版软件中常见的一些错误代码: 1. ...
- OGNL表达式
一.什么是OGNL,有什么特点? OGNL(Object-Graph Navigation Language),大概可以理解为:对象图形化导航语言.是一种可以方便地操作对象属性的开源表达式语言 ...
- Python 生产环境MySQL数据库增量备份脚本
MySQL数据库常用的办法是通过MySQLdump导出sql进行备份,但是不适合数据量很大的数据库,速度,锁表是两个严重的问题.前面写了一遍文章介绍xtrabackup的热备工具,见 http://w ...
- Effective C++ -----条款11: 在operator=中处理“自我赋值”
确保当对象自我赋值时operator=有良好行为.其中技术包括比较“来源 对象”和“目标对象”的地址.精心周到的语句顺序.以及copy-and-swap. 确定任何函数如果操作一个以上的对象,而其中多 ...
- 从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中
1,实现功能:从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中 并实现app向后移动一个元素的位置: 2.实现思路: 01.遍历下拉菜单,添加拖拽方法,实现位置移动功能: 02.遍历app列 ...
- code vs1517 求一次函数解析式(数论 纯数学知识)
1517 求一次函数解析式 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 相信大家都做过练 ...