第一题:遍历链表,遇到重复节点就连接到下一个。

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if (head==null||head.next==null) return head;
  3. ListNode res = head;
  4. while (head.next!=null){
  5. if (head.val==head.next.val)
  6. {
  7. if (head.next.next!=null) head.next = head.next.next;
  8. else head.next = null;
  9. }
  10. else head = head.next;
  11. }
  12. return res;
  13. }

第二题:思路比较简单,设置一个超前节点作为head的前节点,往下遍历,遇到重复的就把超前节点连接到新的val节点。

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if(head==null||head.next==null) return head;
  3. ListNode res = new ListNode(0);
  4. res.next = head;
  5. ListNode list = res;
  6. while (res.next!=null&&res.next.next!=null)
  7. {
  8. ListNode temp = res.next.next;
  9. if (res.next.val==res.next.next.val)
  10. {
  11. while (temp.next!=null&&temp.next.val==temp.val)
  12. {
  13. temp = temp.next;
  14. }
  15. if (temp.next!=null) res.next = temp.next;
  16. else res.next = null;
  17. }
  18. else res =res.next;
  19. }
  20. return list.next;
  21. }

当要经常删除第一个节点是,要设置一个超前节点

[leetcode]82. Remove Duplicates from Sorted List的更多相关文章

  1. [LeetCode] 82. Remove Duplicates from Sorted List II_Medium tag: Linked List

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinctnumbe ...

  2. [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 ...

  3. [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. [LeetCode#82]Remove Duplicates from Sorted Array II

    Problem: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? F ...

  5. leetcode 82. Remove Duplicates from Sorted List II

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

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

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

  8. 82. Remove Duplicates from Sorted List II && i

    题目 83. Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such tha ...

  9. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

随机推荐

  1. Spring Boot系列 八、集成Kafka

    一.引入依赖 <dependency> <groupId>org.springframework.kafka</groupId> <artifactId> ...

  2. xargs--冬天里的一丝暖意

    本文为博客园作者所写: 一寸HUI,个人博客地址:https://www.cnblogs.com/zsql/ 你有批量kill作业吗?有因为删除文件夹的内容太多而报错吗?-bash: /bin/rm: ...

  3. 74CMS3.0储存型XSS漏洞代码审计

    发现一个总结了乌云以前代码审计案例的宝藏网站:https://php.mengsec.com/ 希望自己能成为那个认真复现和学习前辈们思路的那个人,然后准备慢慢开始审计一些新的小型cms了 骑士cms ...

  4. 题解-CF429C Guess the Tree

    题面 CF429C Guess the Tree 给一个长度为 \(n\) 的数组 \(a_i\),问是否有一棵树,每个节点要么是叶子要么至少有两个儿子,而且 \(i\) 号点的子树大小是 \(a_i ...

  5. Mysql为什么使用b+树,而不是b树、AVL树或红黑树?

    首先,我们应该考虑一个问题,数据库在磁盘中是怎样存储的?(答案写在下一篇文章中) b树.b+树.AVL树.红黑树的区别很大.虽然都可以提高搜索性能,但是作用方式不同. 通常文件和数据库都存储在磁盘,如 ...

  6. python叠加矩形框图层

    两种方式以及效果: 方式一,使用PIL.Image.blend方式: from PIL import Image, ImageDraw im = Image.open('d:/tmp/58.249.0 ...

  7. Springboot mini - Solon详解(五)- Solon扩展机制之Solon Plugin

    Springboot min -Solon 详解系列文章: Springboot mini - Solon详解(一)- 快速入门 Springboot mini - Solon详解(二)- Solon ...

  8. django APIview使用

    1.APIview使用 ModelVIewSet 是对 APIView 封装 ModelSerializer 是对 Serializer 1.1 在 user/urls.py 中添加路由 urlpat ...

  9. Validated 注解完成 Spring Boot 参数校验

    1.  @Valid 和 @Validated @Valid 注解,是 Bean Validation 所定义,可以添加在普通方法.构造方法.方法参数.方法返回.成员变量上,表示它们需要进行约束校验. ...

  10. vue第二单元(webpack的配置-学习webpack的常用配置)

    第二单元(webpack的配置-学习webpack的常用配置) #课程目标 掌握webpack的常用配置 掌握如何根据实际的需求修改webpack的对应配置 了解webpack-dev-server的 ...