[LeetCode] Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
这题比较简单,用两个指针,一个cursor遍历链表用,一个stick指向上一个不重复的node,这样每次遍历时如果cursor跟stick不一样的话就把stick的next指向cursor,然后更新stick,这样一直到遍历结束,还有最后一步很容易遗漏,就是把stick指向NULL,因为此时stick应该是新链表的尾巴。
这个方法能适用是因为链表是有序的。
ListNode *deleteDuplicates(ListNode *head) {
if (!head) return NULL; ListNode *stick = head, *cursor = head->next;
while (cursor) {
if (cursor->val == stick->val) {
cursor = cursor->next;
continue;
}
else {
stick->next = cursor;
stick = cursor;
}
cursor = cursor->next;
}
stick->next = NULL;
return head;
}
[LeetCode] Remove Duplicates from Sorted List的更多相关文章
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [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 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [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 List II 解题报告
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- func_num_args, func_get_arg, func_get-args 的区别与用法
func_num_args 返回传递给函数的参数个数 <?php header("Content-Type: text/html; charset=UTF-8"); func ...
- Intellij IDEA使用总结
查询快捷键CTRL+N 查找类CTRL+SHIFT+N 查找文件CTRL+SHIFT+ALT+N 查 找类中的方法或变量CIRL+B 找变量的来源CTRL+ALT+B 找所有的子类CTRL ...
- powerdesigner反向
1.Could not initial JVM不能初始化 :要求安装32位的JDK,把path指向32的安装目录 2.Non SQL Error : Could not load class orac ...
- 基于用户相似性的协同过滤——Python实现
代码基本来自项亮的<推荐系统实践>,把书上的伪代码具体实现,还参考了https://www.douban.com/note/336280497/ 还可以加入对用户相似性的归一化操作,效果会 ...
- NET程序内存分析工具CLRProfiler的使用(性能测试)
http://blog.csdn.net/wy3552128/article/details/8158938 大家都知道.net有一套自己的内存(垃圾)回收机制,除非有一些数据(方法)长期占有内存不随 ...
- 【架构】Google的大规模集群管理工具Borg
参考资料: http://www.cnblogs.com/YaoDD/p/5374393.html http://www.cnblogs.com/YaoDD/p/5351589.html
- POJ 1016
http://poj.org/problem?id=1016 一道字符串处理的题目,理解题意后注意细节就好. 题意:每一串数字 都可以写成 a1 b1 a2 b2 ....ai bi 其中ai是指bi ...
- struts2 模型驱动
public class User3Action extends ActionSupport implements ModelDriven<User> { private User use ...
- 基础02 Java 跨平台原理
1993 , JAVA初衷: 机顶盒 1994 年互联网刚刚兴起,.(高司令\ 高斯林),改造成了面向互联网的计算机语言.java重要特性之 ------- 跨平台(一次编译,到处运行).平台:操作系 ...
- 事件查看器事件ID部分说明
事件查看器从简单的查看电脑登录信息到检查系统是否出现错误,是否被入侵都有着很重要的作用,Microsoft为了简便,采用事件ID来代表一些信息,下面是我从Microsoft找来的WIN2003的对应关 ...