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 ...
随机推荐
- Validation Rule和Binding Group
WPF学习之绑定-Validation Rule和Binding Group 在上一篇文章中我们讨论了有关WPF绑定的知识点,现在我们可以很容易的将业务数据作为源绑定到WPF控件并可以通过创建不同的D ...
- dubbo注册服务IP解析异常及IP解析源码分析
在使用dubbo注册服务时会遇到IP解析错误导致无法正常访问. 比如: 本机设置的IP为172.16.11.111, 但实际解析出来的是180.20.174.11 这样就导致这个Service永远也无 ...
- Powerdesigner中如何生成测试数据
设计表完成以后,我们需要生成一些测试数据,可以直接更新到数据库中,下面我们就来试试: 第一步:建立需要的Profiles测试文件,[Model]--[Test Data Profiles],如图所示: ...
- SQL语句中格式化时间
给数据库中的字段格式化(): to_char(CREATETIME,'yyyy-MM-dd') 给程序中的字段格式化(InTime为数据库字段): InTime = to_date( '" ...
- 借鉴网上的winform模仿QQ窗口停靠功能稍作改动
2015-07-11 15:24:04 1 using System; using System.Collections.Generic; using System.ComponentModel; u ...
- C# Java DES加密解密
转自http://www.cnblogs.com/zhuiyi/archive/2013/04/01/2993201.html 最近被DES加解密弄得超级郁闷,我用C#的方法加密得到的密文老是跟客户给 ...
- [字符串] AppMessage--字符串返回帮助类 (转载)
点击下载 AppMessage.rar 这个类是关于AppMessage的帮助类,主要是格式化返回字符串的里面会有很多的提供字符的格式以及预设置内容,大家可以直接使用的看下面代码吧 /// <s ...
- 【html】【4】html事件集合
必看参考: http://www.runoob.com/tags/ref-eventattributes.html http://www.cnblogs.com/jiangchongwei/archi ...
- zoj1276矩阵连乘dp
很经典的入门dp /*******************************************************************************/ /* OS : 3 ...
- 环状DNA序列
大意: 一个DNA序列是环状的,这意味着有N个碱基的序列有N种表示方法(假设无重复).而这N个序列有一种最小的表示,这个最小表示的意思是这个序列的字典序最小(字典序的意思是在字典中的大小 比如ABC& ...