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 ...
随机推荐
- VC++ 获取windows系统的版本类型
vc中获取windows版本信息,一般是调用GetVersionEx 这个API函数来获取的,这个API需要OSVERSIONINFOEX 这个结构体作为参数,OSVERSIONINFOEX 的对应的 ...
- Android(java)学习笔记194:ListView编写步骤(重点)
1.ListView在我们的手机android编写程序中使用是十分广泛的,比如如下图中 短信 和 手机设置 都是ListView的效果: 手机设置: 短信: 2.正因为这 ...
- 读者汇app项目案例源码
这款app是我花一周左右时间做出来的,一款阅读笑话,段子,糗事,脑筋急转弯,神回复,语录,谜语等的休闲娱乐app,用户除了可以浏览他人发布的内容外,自己也可以发布相关内容,和其他人一同分享有趣的内容, ...
- 必应代码搜索 Bing Code Search 安装
微软这几天推出基于bing搜索引擎的 Bing Code Search ,可直接在浏览器上搜索和运行代码.目前中文版必应无法使用本功能,有需要的同学可以转到英文版进行搜索: 英文版必应: http ...
- fseek()
原文地址:fseek()作者:xiaoxin 意思是把文件指针指向文件的开头 fseek 函数名: fseek 功 能: 重定位流上的文件指针 用 法: int fseek(FILE *s ...
- (转)css换行样式:word-wrap同word-break的区别
以下是引用片段: word-wrap:break-word; overflow:hidden; 而不是 以下是引用片段: word-wrap:break-word; word-break:break- ...
- 虚拟机 Linux 系统增加换页空间
想在虚拟机里面安装oracle10g,发现默认的swap交换空间不满足最低要求,因为我分配的物理内存是1G,那么就按照要求需要2G的swap交换空间,默认只有1G的交换空间.添加swap交换空间的步骤 ...
- OC基础-day02
#pragma mark - Day02_01_对象的创建与使用 1)如何通过类创建一个对象 1. 类是抽象的,无法直接使用 2. 对象是类的一个具体实现,可以直接使用 3. 语法 类名 *对象名 = ...
- WPF 窗体中的 Canvas 限定范围拖动 鼠标滚轴改变大小
xaml代码: <Canvas Name="movBg"> <Canvas.Background> <LinearGradientBrush EndP ...
- hdoj (1162) 最小生成树
Problem B Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...