码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode-83. Remove Duplicates from Sorted List [C++][Java]


    LeetCode-82. Remove Duplicates from Sorted List IILevel up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

    Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

    Example 1:

    Input: head = [1,2,3,3,4,4,5]
    Output: [1,2,5]
    

    Example 2:

    Input: head = [1,1,1,2,3]
    Output: [2,3]
    

    Constraints:

    • The number of nodes in the list is in the range [0, 300].
    • -100 <= Node.val <= 100
    • The list is guaranteed to be sorted in ascending order.

    【C++】

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode() : val(0), next(nullptr) {}
    7. * ListNode(int x) : val(x), next(nullptr) {}
    8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
    9. * };
    10. */

    1. 循环

    1. class Solution {
    2. public:
    3. ListNode* deleteDuplicates(ListNode* head) {
    4. ListNode *dummy = new ListNode(-1), *cur = dummy;
    5. dummy->next = head;
    6. while (cur->next && cur->next->next) {
    7. if (cur->next->val == cur->next->next->val) {
    8. int x = cur->next->val;
    9. while (cur->next && cur->next->val == x) {
    10. //ListNode* freeNode = cur->next;
    11. cur->next = cur->next->next;
    12. //delete freeNode;
    13. //freeNode == nullptr;
    14. }
    15. } else {
    16. cur = cur->next;
    17. }
    18. }
    19. return dummy->next;
    20. }
    21. };

    2. 递归

    1. class Solution {
    2. public:
    3. ListNode* deleteDuplicates(ListNode* head) {
    4. if (head == nullptr || head->next == nullptr) {return head;}
    5. if (head->val != head->next->val) {
    6. head->next = deleteDuplicates(head->next);
    7. return head;
    8. }
    9. ListNode* tmp = head->next;
    10. while (tmp != nullptr && tmp->val == head->val) {tmp = tmp->next;}
    11. return deleteDuplicates(tmp);
    12. }
    13. };

    【Java】

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode() {}
    7. * ListNode(int val) { this.val = val; }
    8. * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    9. * }
    10. */

    1. 循环

    1. class Solution {
    2. public ListNode deleteDuplicates(ListNode head) {
    3. ListNode dummy = new ListNode(-1), cur = dummy;
    4. dummy.next = head;
    5. while (cur.next != null && cur.next.next != null) {
    6. if (cur.next.val == cur.next.next.val) {
    7. int x = cur.next.val;
    8. while (cur.next != null && cur.next.val == x) {
    9. cur.next = cur.next.next;
    10. }
    11. } else {
    12. cur = cur.next;
    13. }
    14. }
    15. return dummy.next;
    16. }
    17. }

    2. 递归

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

    类似题目

    LeetCode-83. Remove Duplicates from Sorted List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a sorted linked list,delete all duplicates such that each element appears only once. Returnthe linked listsortedas well.https://blog.csdn.net/qq_15711195/article/details/122394297

  • 相关阅读:
    Lyapunov optimization 李雅普诺夫优化
    区块链国际会议汇总
    微调stable diffusion哪个部分才是最有效的?
    全量知识系统 程序详细设计 “智能”分类及标注的主题词架构(Q&A百度搜索)
    为了摆脱 Android ,亚马逊开发基于Linux的操作系统
    面试题:JDK、JRE、JVM三者的区别与联系+什么是字节码+采用字节码的好处是什么+List和Set的区别+ArrayList和LinkedList区别
    Python常见工厂函数用法
    java汉字转拼音pinyin4j-2.5.0.jar用法
    淘宝商品详情 API 返回值说明
    利用python中if函数判断三角形的形状
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126191494
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号