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 ...
随机推荐
- Java基础知识强化之集合框架笔记50:Map集合之Map集合的概述和特点
1. Map集合的概述: public interface Map<K,V> 作为学生来说,是根据学号来区分不同的学生的,那么假设我现在已经知道了学生的学号,我要根据学号去获取学生姓名,请 ...
- MYSQL之高级查询
PHP高级查询 分组查询.联合查询.连接查询.子查询 版权声明:本文为博主原创文章,未经博主允许不得转载.
- CI框架篇之模型篇--AR操作(2)
CodeIgniter 和众多的框架一样,有属于自己的一套对数据库的操作方式,本框架更是如此 有属于自己的一套对数据库的安全并且简单的操作, 成为AR操作:下面来对AR操作进行介绍: 首先,确定要启动 ...
- [上传下载] C#修改DownLoadHelper上传下载帮助类 (转载)
点击下载 DownLoadHelper.rar 主要功能如下 /// <summary> /// 输出硬盘文件,提供下载 支持大文件.续传.速度限制.资源占用小 /// </summ ...
- Chart图形 [GDI+] OWCChart统计图的封装类 (转载)
点击下载 OWCChart.zip 利用OWC11进行作统计图的封装类. /// <summary> /// 类说明:进行作统计图的封装类 /// 联系方式:361983679 /// 更 ...
- java中时间差计算
public class Utill { public String TimeString(Date currentTime, Date beginTime){ /*默认为毫秒,除以1000是为了转换 ...
- c#静态成员和静态类
说起静态类,你可能会联想到实例类.这两者并不难区分,前者(静态类)只在内存中创建一个,而后者(实例类)则是每次实例化后,就会再内存创建一份.今天来简单聊一下静态类的理解. 代码情景: class Pr ...
- ubuntu亮度无法自动调节终极解决方案
友情链接:IT狂人博客 这个问题纠结了我快两年,主要是自己懒,写了个脚本来调节亮度,不过还是稍显不便.近日兴起折腾了一番,终于找到问题根结了. There are many ways to contr ...
- Azure cache 的配置与应用
最近公司的项目要是用cloud Service 所以研究了下 Azure cache 的配置与使用. 首先创建项目 第二步 配置 cache worker role (1) 点击 cache work ...
- a*b(高进度乘以int类型的数)
以下是我今日的a-b(高精度)的程序,\(^o^)/偶也偶也偶也偶也! 程序: #include<stdio.h> #include<string.h> char s[1000 ...