题目地址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. android学习笔记——利用BaseAdapter生成40个列表项

    RT: main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  2. [一起读源码]走进C#并发队列ConcurrentQueue的内部世界 — .NET Core篇

    在上一篇<走进C#并发队列ConcurrentQueue的内部世界>中解析了Framework下的ConcurrentQueue实现原理,经过抛砖引玉,得到了一众大佬的指点,找到了.NET ...

  3. Map使用foreach遍历方式,Map获取第一个键值

    List<Map<String, Object>> mapList = new ArrayList<>();  for (Map.Entry<String,O ...

  4. 利用Putty建立SSH的tunnels访问内网资源

    适用场景访问阿里或者腾讯云只针对内网开放的资源. 本文以SQLSERVER 举例 举例你的内网 SQLSERVER的访问地址是192.168.33.88 . 你的Microsoft SQL Serve ...

  5. Spring Boot 中使用自定义注解,AOP 切面打印出入参日志及Dubbo链路追踪透传traceId

    一.使用背景 开发排查系统问题用得最多的手段就是查看系统日志,在分布式环境中一般使用 ELK 来统一收集日志,但是在并发大时使用日志定位问题还是比较麻烦,由于大量的其他用户/其他线程的日志也一起输出穿 ...

  6. JS-Array-新增方法

    1. filter( ) var arr = [5,4,3,2,1]; newarr = arr.filter((item)=>{ return item<3 }) ;  // => ...

  7. [PHP] 获取IP 和JS获取IP和地址

    通过js获取 服务器 ip 服务器端口 服务器地址 var address=window.location.href; thisDLoc = document.location; var hostpo ...

  8. Lesson0423

    ABS计算几何科普讲座 讲个笑话,ABS又来科普简单知识了 %%% STO szO ABS Orz OTZ %%% 之前完全没有接触过计算几何啊...ABS聚聚给我们从最简单的部分讲起,听得还是很爽的 ...

  9. Springboot:员工管理之国际化(十(3))

    1:IDEA编码设置UTF-8 2:创建国际化文件 i18n\login.properties #默认语言 i18n\login_en_US.properties #英文语言 i18n\login_z ...

  10. 解决sublime打开文档,出现中文乱码问题

    sublime text 软件中出现中文乱码,大多是因为编码格式不支持,需要安装一个插件就可以解决中文乱码问题,推荐安装 ConvertToUtf8  安装步骤: 1.按“shift + ctrl + ...