LeetCode--LinkedList--83.Remove Duplicates from Sorted List(Easy)
题目地址https://leetcode.com/problems/remove-duplicates-from-sorted-list/
83. Remove Duplicates from Sorted List(Easy)
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
solution
我的解法
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head){
ListNode p = head , t = head; //p指向当前结点,t指向去重后的尾结点
while (p != null && p.next != null)
{
while (p.next != null && p.val == p.next.val) //找到下一个结点值不重复的结点p.next
p = p.next;
if (p.next != null) //将p.next连在t后面
{
p = p.next;
t.next = p;
t = p;
}
else //如果p.next为null,说明去重已完成
t.next = null;
}
return head;
}
}
官方解法
public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;
while (current != null && current.next != null) {
if (current.next.val == current.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
}
reference
https://leetcode.com/problems/remove-duplicates-from-sorted-list/solution/
总结
题意是给定一个有序链表,要求删掉里面重复的元素。
- 我的思路是用while循环遍历链表,接着用一个while循环找到与当前结点值不同的下一个结点,然后将找到的结点与当前新链表的尾结点连接起来,直到外层while循环结束.
- 官方思路简单粗暴,直接比较当前结点与下一结点的值,如果相等,则将当前结点与下一结点的下一结点连接起来,否则继续检查下一结点.
Notes
1.这种题感觉没必要想的很复杂,我就是考虑复杂了,导致我的解法比官方解法慢.
2.链表的插入与删除要特别注意头结点与尾结点.
LeetCode--LinkedList--83.Remove Duplicates from Sorted List(Easy)的更多相关文章
- 【一天一道LeetCode】#83. Remove Duplicates from Sorted List
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...
- 【LeetCode】83 - 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
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- LeetCode OJ 83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- <LeetCode OJ> 83. Remove Duplicates from Sorted List
83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
随机推荐
- python批量添加hexo文章封面
❝ 本文需要工具: 「excel」 「python3.x」 ❞ 今天突然觉得,我的博客的文章更新这么多了竟然还没有一个封面,觉得首页相当低调了- 首页 正好皮肤带有文章封面功能,所以我觉得要将文章批量 ...
- Daily Scrum 12/29/2015
Process: Zhaoyang: Add the Time bar feature in the APP and complete the Speech API. Yandong: Do some ...
- D. Points in rectangle
D. Points in rectangle 单点时限: 2.0 sec 内存限制: 512 MB 在二维平面中有一个矩形,它的四个坐标点分别为(0,a),(a,0),(n,n−a),(n−a,n). ...
- PHP函数:fopen
fopen() - 打开文件或者 URL. 注意:array_key_exists() 仅仅搜索第一维的键. 多维数组里嵌套的键不会被搜索到. 说明: fopen ( string $filenam ...
- centos7在命令行下安装图形界面
yum groupinstall "GNOME Desktop" "Graphical Administration Tools" ln -sf /lib/sy ...
- vagrant + 宝塔 环境搭建遇到的一些问题
1.js时间戳单位为毫秒,php时间戳为秒. 2.tp里的where()方法如果where("id=".$id)不能用的话就用数组形式的:where(array("id& ...
- 2019-2020-1 20199329《Linux内核原理与分析》第一周作业
Linux学习随笔 Linux 是一个操作系统,我们的 Linux 主要是系统调用和内核那两层. UNIX前身是Multics,但 UNIX 的商业版本非常昂贵,于是Linus Torvalds(Li ...
- [Inno Setup] 如何读取命令行输入的参数
以 /verysilent 为例 [Code] var isVerySilent: Boolean; function InitializeSetup(): Boolean; var j: Integ ...
- java之重载与重写
重写(override) 重写是子类对父类的允许访问的方法的重新编写,方法名,返回值类型和形参列表都不能改变,唯一恩能够改变的是方法体. 重写的好处是可以根据子类的需要的行为来实现父类的方法. 重写方 ...
- pip安装openvc-python国内镜像源
采用清华大学的镜像源. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghu ...