LeetCode OJ--Remove Duplicates from Sorted List II *
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
处理链表的范例
#include <iostream>
using namespace std; 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 dummy(INT_MIN);
dummy.next = head;
ListNode *prev = &dummy,*cur = head;
while(cur!= NULL)
{
bool duplicated = false;
while(cur->next != NULL && cur->val == cur->next->val)
{
duplicated = true;
cur = cur->next;
}
if(duplicated)
{
cur = cur->next;
continue;
}
prev->next = cur;
prev = prev->next;
cur = cur->next;
}
prev->next = cur;
return dummy.next;
}
}; int main()
{
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode();
ListNode *n4 = new ListNode();
ListNode *n5 = new ListNode();
ListNode *n6 = new ListNode();
ListNode *n7 = new ListNode();
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = n6;
n6->next = n7;
ListNode *ans;
Solution myS;
ans = myS.deleteDuplicates(n1);
return ;
}
LeetCode OJ--Remove Duplicates from Sorted List II *的更多相关文章
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 【leetcode】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [leetcode] 80. Remove Duplicates from Sorted Array II (Medium)
排序数组去重题,保留重复两个次数以内的元素,不申请新的空间. 解法一: 因为已经排好序,所以出现重复的话只能是连续着,所以利用个变量存储出现次数,借此判断. Runtime: 20 ms, faste ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 快学UiAutomator各种框架介绍
Monkey 编写语言:命令行 运行环境:使用adb连接PC运行测试对象:Android平台自动化测试的一种手段,通过Monkey程序模拟用户触摸屏幕.滑动Trackball.按键等操作来对设备上的程 ...
- CPP-练习
HW: 1.局部变量能否和全局变量重名? 答:能,局部会屏蔽全局.要用全局变量,需要使用"::" ;局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会 ...
- shell脚本,awk实现每个数字加1.
[root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...
- Java--返回类的对象(return this)
如下代码所示: public Book getBook(){ return this; } 在getBook()方法中,方法的返回值为Book类,所以方法体中使用 return this 这种形式返回 ...
- [LUOGU] P3128 [USACO15DEC]最大流Max Flow
题意:一棵树,多次给指定链上的节点加1,问最大节点权值 n个点,n-1条边很容易惯性想成一条链,幸好有样例.. 简单的树剖即可!(划去) 正常思路是树上差分,毕竟它就询问一次.. #include&l ...
- mysql5.7.22-log 修改远程访问
正常的设置账号远程访问依然访问不了的情况,可以看一下服务器 my.cnf配置文件下 [client] #password = your_password 把上面的#去掉就行了.
- 五:SQL语句中的数据类型
一:MySQL数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的 MySQL支持多种数据类型,大致可以分为三类:数值 日期/时间和字符串 二.数值类型(12) 2.1.整数类型(6) ...
- Linux基础学习-Docker学习笔记
Docker安装 1 官方网站访问速度很慢,帮助文档 2 国内中文网站,帮助文档 [root@qdlinux ~]# yum remove docker \ docker-client \ docke ...
- 【php】 php的注释问题,单行注释和多行注释与php结束符的关系
单行注释仅仅注释到行末或者当前的 PHP 代码块,视乎哪个首先出现.这意味着在 // ... ?> 或者 # ... ?> 之后的 HTML 代码将被显示出来:?> 跳出了 PHP ...
- 《零基础入门学习Python》【第一版】视频课后答案第004讲
1.while语句中,当条件为真时,它会一直循环下去,比如下面的例子,不过可以用Ctral + C来强制结束 while 'C': print("i love you") 2.观察 ...