[leetcode]remove-duplicates-from-sorted-list-ii

. - 力扣(LeetCode)

给定一个已排序的链表的头 head删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表

示例 1:

复制代码
输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]

示例 2:

复制代码
输入:head = [1,1,1,2,3]
输出:[2,3]

提示:

  • 链表中节点数目在范围 [0, 300]

  • -100 <= Node.val <= 100

  • 题目数据保证链表已经按升序 排列

    class Solution {
    public:
    ListNode* deleteDuplicates(ListNode* head) {
    if (!head) {
    return head;
    }

    复制代码
          ListNode* dummy = new ListNode(0, head);
    
          ListNode* cur = dummy;
          while (cur->next && cur->next->next) {
              if (cur->next->val == cur->next->next->val) {
                  int x = cur->next->val;
                  while (cur->next && cur->next->val == x) {
                      cur->next = cur->next->next;
                  }
              }
              else {
                  cur = cur->next;
              }
          }
    
          return dummy->next;
      }

    };

相关推荐
用户2367829801688 分钟前
从 chmod 755 说起:Unix 文件权限到底是怎么算的?
linux
Strugglingler14 分钟前
【systemctl 学习总结】
linux·systemd·systemctl·journalctl·unit file
嵌入式×边缘AI:打怪升级日志2 小时前
100ASK-T113 Pro 开发板 Bootloader 完全开发指南
linux·ubuntu·bootloader
水蓝烟雨2 小时前
1931. 用三种不同颜色为网格涂色
算法·leetcode
charlie1145141913 小时前
Linux 字符设备驱动:cdev、设备号与设备模型
linux·开发语言·驱动开发·c
handler013 小时前
Linux 内核剖析:进程优先级、上下文切换与 O(1) 调度算法
linux·运维·c语言·开发语言·c++·笔记·算法
zhouwy1133 小时前
Linux进程与线程编程详解
linux·c++
我星期八休息3 小时前
IT疑难杂症诊疗室:AI时代工程师Superpowers进化论
linux·开发语言·数据结构·人工智能·python·散列表
切糕师学AI4 小时前
深入解析 Zsh 与 Oh-My-Zsh:打造高效现代化终端
linux·终端·zsh
leoufung4 小时前
LeetCode 76:Minimum Window Substring 题解与滑动窗口思维详解
算法·leetcode·职场和发展