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 ...
随机推荐
- 仿QQ迷你首页(VC++,MFC)(迷你资讯)的开发与实现(源代码)
由于需求,需要写个类似QQ迷你资讯首页的东西,就花了一点时间写了写,软件效果截图如下: 程序的主要核心代码如下: 程序的全部源代码下载地址:http://download.csdn.net/downl ...
- servlet登录
package com.lxr.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Pre ...
- memcached 安装
安装 memcached 需要 三部1,下载 memcached 放到php目录将php_memcached.dll 放到php的ext 目录 2,打开管理员命令,将memcached 拖拉到命令中, ...
- [Cookie] C#CookieHelper--C#操作Cookie的帮助类 (转载)
点击下载 CookieHelper.rar 下面是代码大家看一下 /// <summary> /// 类说明:CookieHelper /// 联系方式:361983679 /// 更新网 ...
- jmeter中webdriver插件,进行自动化压测
1.下载JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache-jmeter ...
- 初学时的shell
学习期间写过一些shell脚本, 测试过程:vi test.sh 后把程序写入其中,保存退出.然后改变文件属性:chmod +x test.sh 最后执行:./test.shfor语句测试:1)#!/ ...
- raw socket遇上windows
最近很长一段时间内又捡起了大学时丢下的网络协议,开始回顾网络协议编程,于是linux系统成了首选,它让我感到了无比的自由,可以很通透的游走于协议的各层. 最初写了个ARP欺骗程序,很成功的欺骗了win ...
- eclipse EE neon创建dynamic web project时,卡在installing dynamic web module facet,解决办法
我们在用eclipse EE neon创建dynamic web project时,如果你发现底部状态栏一直卡在installing dynamic web module facet,永远到不了100 ...
- POJ 3181 Dollar Dayz(高精度 动态规划)
题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...
- PHP设计模式之:装饰模式
<?php// 人类class Person{ private $name; public function __construct($name) { $this ...