[LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)
描述
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
有序链表去重。
解析
移除给定有序链表的重复项,那么我们可以遍历这个链表,每个结点和其后面的结点比较,如果结点值相同了,我们只要将前面结点的next指针跳过紧挨着的相同值的结点,指向后面一个结点。这样遍历下来,所有重复的结点都会被跳过,留下的链表就是没有重复项的了。
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
//me
public ListNode deleteDuplicates(ListNode head) {
if (null == head) {
return head;
}
ListNode cur = head;
ListNode next = cur.next;
while (cur != null && next != null) {
while (next != null && cur.val == next.val) {
cur.next = next.next;
next = next.next;
}
cur = cur.next;
}
return head;
}
}
相似问题:删除链表中的重复节点 (难一点)
[LeetCode] 83. Remove Duplicates from Sorted List ☆(从有序链表中删除重复项)的更多相关文章
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- 026 Remove Duplicates from Sorted Array 从排序数组中删除重复项
给定一个有序数组,你需要原地删除其中的重复内容,使每个元素只出现一次,并返回新的长度.不要另外定义一个数组,您必须通过用 O(1) 额外内存原地修改输入的数组来做到这一点.示例:给定数组: nums ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- LeetCode 82,考察你的基本功,在有序链表中删除重复元素II
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题的第51篇文章,我们来看LeetCode第82题,删除有序链表中的重复元素II(Remove Duplicates ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
随机推荐
- JVM探秘6--图解虚拟机栈的局部变量表和操作数栈工作流程
案例代码如下: public class JVMTest { public static Integer num = 10; public int add(int i){ int j = 5; int ...
- jquery的cookie插件
一.JS文件 /*! * jQuery Cookie Plugin v1.4.1 * https://github.com/carhartl/jquery-cookie * * Copyright 2 ...
- wpf中插入winform控件并获取句柄
因工作需要使用wpf做界面,而有个开发包依赖picturebox控件,上网研究了一下,总算弄通了. 首先在项目中添加引用System.Windows.Forms与WindowsFormsIntegra ...
- SNMP理解
前两天项目要求一个附加功能,远程监视服务器的运行状况,要定期监视指定端口,指定业务,还包括服务器的磁盘空间,内存,CPU使用率等等.这头俩事还好说,ping和telnet也就搞定了,实在不行就开个so ...
- 《CSS世界》读书笔记(十六)
<!-- <CSS世界>张鑫旭著 --> line-height与“垂直居中” line-height 可以让单行或多行元素近似垂直居中,原因在于 CSS 中“行距的上下等分机 ...
- QT 添加外部库文件
LIBS += D:\Code\Opengltest\OpenGL32.Lib D:\Code\Opengltest\GlU32.Lib LIBS += OpenGL32.Lib GlU32.Lib ...
- 面试-java反射
问题:简述Java中的反射使用 答: 1.作用: 可以通过配置文件来动态配置和加载类,以实现软件工程理论里所提及的类与类,模块与模块之间的解耦.反射最经典的应用是spring框架. 2. 定义 反射简 ...
- 基础JAVA程序设计(多个类与方法的实现2)
设计一个类代表二维空间的一个点(Point),要求:两个成员变量:x坐标和y坐标. 设计一个类代表二维空间的一个圆(Circle),要求:两个成员变量:一个是圆心,一个是半径:提供计算圆面积的方法:提 ...
- 清除本地SVN信息
C:\Documents and Settings\yangxf\Application Data\Subversion\auth 这个目录下删除svn文件夹即可
- 强杀apt-get install进程导致错误的修复办法
关闭了一个安装缓慢的apt-get install终端窗口,安装另一个软件,提示打不开var下的一个锁(没加sudo也是这个错误但会提示是非root用户权限不够导致,但这里不是这个问题),说是另一个进 ...