lc面试准备:Remove Duplicates from Sorted List
1 题目
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
.
接口
ListNode deleteDuplicates(ListNode head)
简单的链表操作的题目,要求是删去有序链表中重复的元素。
2 思路

复杂度
3 代码
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(Integer.MAX_VALUE);
ListNode tail = dummy;
ListNode pCur = head;
while(pCur != null){
if(tail.val != pCur.val){
tail.next = pCur;
tail = pCur;
}
pCur = pCur.next;
}
tail.next = null;
return dummy.next;
}
4 总结
- 和从数组中移除重复元素那一题一样的思想。
- 链表的操作在面试中考察不多。
5 扩展
6 参考
lc面试准备:Remove Duplicates from Sorted List的更多相关文章
- [LC]83题 Remove Duplicates from Sorted List(删除排序链表中的重复元素)(链表)
①英文题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exa ...
- [LC]26题 Remove Duplicates from Sorted Array (删除排序数组中的重复项)(双指针法)(原地实现)
①中文题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- lc面试准备:Remove Duplicates from Sorted List II
1 题目 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct ...
- [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-83 Remove Duplicates from Sorted List
#83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such that ...
- Remove Duplicates from Sorted List II
Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...
随机推荐
- Builder 生成器模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 当同时满足以下情况的时候可以使用Builder模式: 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式. 当 ...
- Linux安装Jdk,CentOS安装Jdk
Linux安装Jdk,CentOS安装Jdk >>>>>>>>>>>>>>>>>>>& ...
- Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致
Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null
- http://bbs.phpcms.cn/thread-266337-1-1.html
http://bbs.phpcms.cn/thread-266337-1-1.html utf8 改成 ANSI”
- SQL多行拼接为一行
使用简单T-SQL,拼接一列多行为一行.按SQL SERVER的说法叫做自拼接(PS:区分自连接) 还有一种方法是for xml path的方式,感觉不实用. declare @Result varc ...
- IXListView的自我分析一
XListView是一个很不错的用来刷新和加载的控件,下拉刷新和上拉加载.目前这个控件已经没有更新,这个不重要,重要的是它确实还不错,之后可能一直有人在用. android没有提供原生的这类控件,需要 ...
- MongoDB的查询
一.Find操作 二.分页和排序 三.游标的使用 一.Find查询 事前准备:插入如下数据 db.Students.insert([ { _id:1, name:"Zhao", a ...
- wordpress 后台显示空白现象
简单的说两句,出现此种现象的因素可能在于主题或者插件再或者是因为(恶意)插件(误更改)更改了某个重要的文件出现错误.本次我遇到的是插件的错误,具体是什么错误,我也没有去深究,重要的是结果! 使用排查的 ...
- 【HDU1402】【FNT版】A * B Problem Plus
Problem Description Calculate A * B. Input Each line will contain two integers A and B. Process to ...
- Constructor and destructor -- Initialization & Cleanup in C++
Why need initialization and cleanup? A large segment of C bugs occur when the programmer forgets to ...