Remove Duplicates from Sorted List II [LeetCode]
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
.
Solution:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL || head->next == NULL)
return head; //remove the head
ListNode * same_node = NULL;
while(head != NULL) {
if(same_node == NULL) {
if(head->next != NULL && head->val == head->next->val){
same_node = head;
head = head->next;
}else {
break;
}
}else {
if(head->val == same_node->val)
head = head->next;
else
same_node = NULL;
}
} if(head == NULL || head->next == NULL)
return head; ListNode * pre = head;
ListNode * same = NULL;
ListNode * current = pre->next;
while(current != NULL) {
if(same == NULL) {
if(current->next != NULL && current->val == current->next->val){
same = current;
pre->next = current->next;
}else{
pre = pre->next;
}
current = current->next;
}else {
if(current->val == same->val){
pre->next = current->next;
current = current->next;
}else {
same = NULL;
}
}
} return head;
}
Remove Duplicates from Sorted List II [LeetCode]的更多相关文章
- Remove Duplicates from Sorted Array II [LeetCode]
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II ——LeetCode
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- Remove Duplicates from Sorted Array II leetcode java
题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...
- Remove Duplicates from Sorted List II leetcode java
题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- Remove Duplicates from Sorted List II ——LeetCode
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 82. 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II)
82. 删除排序链表中的重复元素 II 82. 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 ...
- 【LeetCode练习题】Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- 权威发布:长链非编码RNA命名规则
转自:http://blog.sina.com.cn/s/blog_8088f3700101pab7.html 权威发布:长链非编码RNA命名规则 对于人类基因命名标准的制定而言,雨果基因命名委员会( ...
- [HDOJ5723]Abandoned country(最小生成树,期望)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5723 题意:求最小生成树,并且求这棵最小生成树上所有边走过次数的期望. 走过次数的期望=Σ边被走过次数 ...
- Intellij IDEA中使用Struts2
据说struts2中有很多的漏洞, 不过作为学习我也就用了吧, 因为书上面是按着这个讲的呀. 难怪官网上也没有struts2.2.1的版本的下载. 1. 下载struts2.2.1 ga版本 2. 新 ...
- EditPlus添加到右键菜单
1.Alt+R 键打开“运行” 2.“运行”中输入:regedit 打开注册表 (1.在 "我的电脑HKEY_CLASSES_ROOT*" 下新建项 shell: (2.在 ...
- UVA 12898 - And Or 数学
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- pfx,cer转pem,并对通过pem文件进行签名与验签
因为PHP无法读取.pfx文件,所以可以先转换为.pem文件然后在读取里面的数据,可以读取.cer文件,为了两方面统一,就都换成.pem然后再进行加签和验签. sign.php <?php de ...
- 工具配置(eclipse/plsql)
PLSQL 附常用配置: PrefAutomaticStatistics=True SelectedStatNames= AutoSelectSQL=True ShowSQLWindowGutte ...
- set使用
package com.cz.test.util.collection; import java.util.ArrayList;import java.util.Collection;import j ...
- css 的小细节,小总结
CSS的一些零碎总结 1.CSS 伪元素用于向某些选择器设置特殊效果(用来当作一个东西的,跟一个元素差不多,但不是元素). ① :frist-line伪元素:用于向文本首行设置特殊样式,但是只能用于块 ...
- ListView 使用详解
在ListView中设置Selector为null会报空指针?mListView.setSelector(null);//空指针试试下面这种:mListView.setSelector(new Col ...