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

    };

相关推荐
kebidaixu4 小时前
BCU 平台 RS485 驱动适配:从 THVD1406 到 ISO3082
linux
谢平康7 小时前
解决用 rm 报bash: /usr/bin/rm: Argument list too long错
linux·运维·运维开发
退休倒计时8 小时前
【每日一题】LeetCode 53. 最大子数组和 TypeScript
数据结构·算法·leetcode·typescript
hj2862518 小时前
Linux 网络服务综合笔记(概念 + 命令 + 实操案例)2
linux·运维·网络
what_20188 小时前
Linux 磁盘 (查看、划分、inode)
linux·运维·服务器
2739920299 小时前
GDB调试(Linux)
linux
凡人叶枫9 小时前
Effective C++ 条款23:宁以 non-member、non-friend 替换 member 函数
linux·开发语言·c++·嵌入式开发
不会C语言的男孩9 小时前
Linux 系统编程 · 第 4 章:文件属性与元数据
linux·c语言·开发语言
小生不才yz10 小时前
Shell脚本精读 · S02-03 | 词拆分、通配符与未加引号的变量
linux
2601_9618454210 小时前
法考真题及答案解析|历年真题|资料已整理
linux·windows·ubuntu·macos·centos·gnu