[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;
      }

    };

相关推荐
乌托邦的逃亡者36 分钟前
Linux中如何检测IP冲突
linux·运维·tcp/ip
一曦的后花园1 小时前
linux搭建promethes并对接node-exporter指标
linux·运维·服务器
乌托邦的逃亡者1 小时前
CentOS/Openeuler主机中,为一个网卡设置多个IP地址
linux·运维·网络·tcp/ip·centos
念恒123062 小时前
进程控制---自定义Shell
linux·c语言
superior tigre2 小时前
78 子集
算法·leetcode·深度优先·回溯
风曦Kisaki2 小时前
# Linux Shell 编程入门 Day02:条件测试、if 判断、循环与随机数
linux·运维·chrome
李日灐3 小时前
< 6 > Linux 自动化构建工具:makefile 详解 + 进度条实战小项目
linux·运维·服务器·后端·自动化·进度条·makefile
嵌入式×边缘AI:打怪升级日志3 小时前
嵌入式Linux开发:开源组件、第三方库与许可证详解
linux
计算机安禾3 小时前
【Linux从入门到精通】第34篇:搭建FTP与Samba——跨平台文件共享解决方案
linux·运维·服务器
日取其半万世不竭3 小时前
用 Netdata 实时监控服务器,比 Prometheus + Grafana 轻量得多
linux·服务器·网络·系统架构·负载均衡·zabbix·grafana