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

    };

相关推荐
zh路西法4 小时前
【navigation2全局路径更新频率修正】行为树框架的巧妙利用
linux
苏宸啊4 小时前
IPC管道
linux·c++
bush44 小时前
嵌入式linux学习记录十,定时器
linux·嵌入式
峥无5 小时前
Linux进程信号:从基础概念到内核底层原理
linux·运维·服务器·信号处理
广州灵眸科技有限公司5 小时前
瑞芯微RV1126B开发板(EASY-EAI-PI2) 开发(编译)方式说明
linux·服务器·单片机·嵌入式硬件·电脑
北山有鸟5 小时前
用开发板的.config替换ubuntu中内核源码目录的.config
linux·运维·ubuntu
8Qi85 小时前
LeetCode 213:打家劫舍 II(House Robber II)—— 题解 ✅
算法·leetcode·职场和发展·动态规划
jcbut6 小时前
离线安装dify 1.7
linux·运维·dify
云计算磊哥@6 小时前
运维开发宝典024-Linux云计算运维入门阶段总结
linux·运维·运维开发
江华森6 小时前
《Linux内核技术实战:从Page Cache到CPU调度的深度解构》博客大纲(26讲精编版)
linux