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

    };

相关推荐
Apipi*3 小时前
30天速通Linux 第六章信号及信号处理
linux·运维·信号处理
天启HTTP4 小时前
爬虫防封核心原理:IP轮换机制与风控规避逻辑
linux·服务器·网络·爬虫·tcp/ip
夏幻灵4 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
广州浮点FLOATLIC6 小时前
许可证服务器迁移后软件打不开:研发 IT 怎样定位连接问题
linux·服务器·数据库
Navigator_Z6 小时前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
BLUcoding6 小时前
接口服务公网超时排查记录:SecureRandom 阻塞问题分析与解决
java·linux·springboot·aes·securerandom
AIgorithmGEEK6 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2087 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)7 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6257 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器