题目地址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. 教你如何在工作中“偷懒”,python优雅的帮你解决

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取htt ...

  2. F - What Is Your Grade?

    “Point, point, life of student!” This is a ballad(歌谣)well known in colleges, and you must care about ...

  3. [转+自]关于PHP7的新特性(涉及取反和disabled_functions绕过)

    PHP7和PHP5上的安全区别 preg_replace()不再支持/e修饰符 利用\e修饰符执行代码的后门大家也用了不少了,具体看官方的这段描述: 如果设置了这个被弃用的修饰符, preg_repl ...

  4. PHP 常用数组的具体运用?常用吗?

    在 PHP 中,有三种类型的数组: 数值数组 - 带有数字 ID 键的数组 关联数组 - 带有指定的键的数组,每个键关联一个值 多维数组 - 包含一个或多个数组的数组 看具体实例: 创建数组 < ...

  5. ADO.NET(二)

     对Command的拓展延伸 执行SQL语句. Command 对象需要取得将要执行的SQL语句,通过调用该类的多种方法,向数据库提交SQL语句. ExecuteNonQuery(),ExecuteR ...

  6. 数值计算方法实验之Hermite 多项式插值 (Python 代码)

    一.实验目的 在已知f(x),x∈[a,b]的表达式,但函数值不便计算,或不知f(x),x∈[a,b]而又需要给出其在[a,b]上的值时,按插值原则f(xi)= yi(i= 0,1…….,n)求出简单 ...

  7. Redis 的 maxmemory 和 dbnum 默认值都是多少?对于最大值会有限制吗?

    一.Redis 的默认配置 了解 Redis 的都知道,Redis 服务器状态有很多可配置的默认值. 例如:数据库数量,最大可用内存,AOF 持久化相关配置和 RDB 持久化相关配置等等.我相信,关于 ...

  8. Jenkins(1)- centos7.X下安装Jenkins

    如果想从头学起Jenkins的话,可以看看这一系列的文章哦 https://www.cnblogs.com/poloyy/category/1645399.html 下载安装包 wget http:/ ...

  9. 2019-2020-1 20199325《Linux内核原理与分析》第九周作业

    第九周作业要求: 理解Linux系统中进程调度的时机,可以在内核代码中搜索schedule()函数,看都是哪里调用了schedule(),判断我们课程内容中的总结是否准确: 使用gdb跟踪分析一个sc ...

  10. 在Spring Boot中使用内存数据库

    文章目录 H2数据库 HSQLDB Apache Derby SQLite 在Spring Boot中使用内存数据库 所谓内存数据库就是可以在内存中运行的数据库,不需要将数据存储在文件系统中,但是相对 ...