题目地址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)的更多相关文章

  1. 【一天一道LeetCode】#83. Remove Duplicates from Sorted List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】83. Remove Duplicates from Sorted List 解题报告(C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断相邻节点是否相等 使用set 使用列表 递归 日 ...

  3. 【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 ...

  4. 【LeetCode】83 - Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  5. 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 ...

  6. 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题要求 ...

  7. <LeetCode OJ> 83. Remove Duplicates from Sorted List

    83. Remove Duplicates from Sorted List Total Accepted: 94387 Total Submissions: 264227 Difficulty: E ...

  8. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  9. 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...

随机推荐

  1. 前端笔记(关于css盒模型知识整理)

    我以前整理的文章可能也不是特别深入.所以现在开始尝试即使多花点时间收集整理,也不只发浅层知识,这样对技术的深入理解是很有帮助的. 废话不多说,我们现在开始. 说到css盒模型,这是大多面试基础中会经常 ...

  2. 同事上班时间无聊,用python敲出贪吃蛇游戏打发时间

    自从学会啦python,再也不用担心上班时间老板发现我打游戏啦 贪吃蛇代码: 还有不懂的(https://www.ixigua.com/i6808019824560570888/)这里有视频教程. 如 ...

  3. Alpha-release 总结

    因组员一周来事情较多,因此博客更新停滞了一个星期.这周我们已经开始了第二个release的相关工作,首先先对上一个release的工作进行简短总结. 团队在上个星期进行了alpha-release版本 ...

  4. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  5. C - Trailing Zeroes (III) 二分

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...

  6. sudo -s 命令 [oh-my-zsh] 提示检测到不安全目录

    运行sudo -s 命令时,[oh-my-zsh] 冒出下面一大堆提示: [oh-my-zsh] Insecure completion-dependent directories detected: ...

  7. testNG 常用的注解

    常用注解介绍: @BeforeSuite 在该套件的所有测试都运行在注释的方法之前,仅运行一次 @AftereSuite 在该套件的所有测试都运行在注释方法之后,仅运行一次 @BeforeClass  ...

  8. [javascript]JS获取当前时间戳的方法

    JavaScript 获取当前时间戳: 第一种方法:(这种方法只精确到秒) var timestamp = Date.parse(new Date()); 结果:1280977330000 第二种方法 ...

  9. Python - Python算法之冒泡算法的超简单实现

    [原创]转载请注明作者Johnthegreat和本文链接 冒泡排序在算法中算是最简单也最容易实现的,这里介绍一个非常简单实现的代码: def bubble_sort(ls): for first in ...

  10. solr管理集合

    其实完全版的管理,在web页面上就有. 同时,在官网文档上,也有:https://lucene.apache.org/solr/guide/6_6/coreadmin-api.html#CoreAdm ...