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 ...
随机推荐
- vsphere 5.1 改进和SSO理解
虚拟交换器 以5.1版的vSphere而言,VMware在VDS上提供一些新功能.例如,现在可以用快照的方式,来备份还原VDS组态及网络端口群组(port group)的组态,以因应vCenter S ...
- 压力测试 JMeter3.3
历史下载版本 https://archive.apache.org/dist/jmeter/source/
- java相关知识集锦
java语言基础知识: Java8 Stream语法详解 不用循环 java 8系列之Stream的基本语法详解 java8 stream filter等功能代替for Java中try catch ...
- scpclient使用报错fchmod无法找到问题解决
因为这个函数时linux下专用的,Windows下无法使用,所以会导致提示这个函数不能使用,解决的方法如下: 1. import platform 2. if platform.system() == ...
- css改变hr颜色
html中用css改变颜色,<hr style="border:0;height:1px;">如果不加border:0;的话,虽然颜色改变了,但是会显示一条黑色的边框. ...
- V-rep学习笔记:机器人模型创建2—添加关节
下面接着之前经过简化并调整好视觉效果的模型继续工作流,为了使模型能受控制运动起来必须在合适的位置上添加相应的运动副/关节.一般情况下我们可以查阅手册或根据设计图纸获得这些关节的准确位置和姿态,知道这些 ...
- python之模块filecmp(文件/目录比较)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块filecmp(文件/目录比较) #用于比较文件及文件夹的内容.他是轻量级的工具.可以做一 ...
- ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)
[root@ldaptest openldap]# ldapadd -x -D "cn=admin,dc=ultrapower,dc=com" -W -f /tmp/base.ld ...
- Oracle数据库查看SID和service_name
怎样查看Oracle的数据库名称sid用sysdba身份登录 比如 conn / as sysdba 匿名管理员登陆执行 select name form V$database; 或是执行selec ...
- 如何查看 EBS 环境上的 INV RUP 版本号
select 'Application Environment: '|| i.instance_name || ', Host: '|| i.host_name ||', Application Re ...