当前位置: 首页 > news >正文

leetcode 83和84 Remove Duplicates from Sorted List 和leetcode 1836

目录

83. Remove Duplicates from Sorted List

82. Remove Duplicates from Sorted List II

1836. Remove Duplicates From an Unsorted Linked List


删除链表中的结点合集

83. Remove Duplicates from Sorted List

代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* deleteDuplicates(ListNode* head) {if(head == nullptr)return head;ListNode* pre = head;ListNode* cur = head->next;ListNode* nex = nullptr;while(cur){nex = cur->next;if(cur->val == pre->val){pre->next = nex;delete cur;cur = nex;}else{pre = cur;cur = nex;}}return head;}
};

82. Remove Duplicates from Sorted List II

 

代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* deleteDuplicates(ListNode* head) {if(head == nullptr)return head;ListNode* dummy = new ListNode(-1,head);ListNode* prepre = dummy;int pre_val = head->val;ListNode* pre = head;ListNode* cur = pre->next;ListNode* nex = nullptr;while(cur){nex = cur->next;if(cur->val == pre_val){prepre->next = nex;if(pre){delete pre;pre = nullptr;}delete cur;cur = nex;}else{if(pre){prepre = pre;}pre = cur;pre_val = pre->val;cur = nex;}}ListNode* ans = dummy->next;delete dummy;return ans;}
};

1836. Remove Duplicates From an Unsorted Linked List

 

代码:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode() : val(0), next(nullptr) {}*     ListNode(int x) : val(x), next(nullptr) {}*     ListNode(int x, ListNode *next) : val(x), next(next) {}* };*/
class Solution {
public:ListNode* deleteDuplicatesUnsorted(ListNode* head) {unordered_map<int,int> count;ListNode* cur = head;while(cur){count[cur->val]++;cur = cur->next;}ListNode* dummy = new ListNode(-1,head);ListNode* pre = dummy;cur = head;ListNode* post = nullptr;while(cur){post = cur->next;if(count[cur->val] > 1){pre->next = post;// delete cur;cur = post;}else{pre = cur;cur = post;}}ListNode* ans = dummy->next;delete dummy;return ans;}
};

 

http://www.dtcms.com/a/206443.html

相关文章:

  • 微服务的应用案例
  • JMeter-Websocket接口自动化
  • C++23中std::span和std::basic_string_view可平凡复制提案解析
  • SpringBoot的前世今生
  • 学习STC51单片机11(芯片为STC89C52RC)
  • 使用VLOOKUP查询两个表格,使用数字格式进行查询,如果返回NA错误,则使用文本格式进行查询
  • 实战:Dify智能体+Java=自动化运营工具!
  • 1 研发规划
  • java基础(方法)
  • Spring Boot——自动配置
  • PL/Python数据库: PostgreSQL Python扩展
  • 2025最新版Visual Studio Code for Mac安装使用指南
  • 代码走读 Go 语言 Map 的实现
  • PyInstaller入门
  • 阿里云服务器 篇十三(加更):Web书签(链接共享和迷你导航):改为使用宿主机DB等优化
  • 第八天 搭建车辆状态监控平台(Docker+Kubernetes) OTA升级服务开发(差分升级、回滚机制)
  • 训练一个线性模型
  • halcon 三维点直线拟合
  • 角度回归——八参数检测四边形RSDet
  • 单例模式的运用
  • Spring Boot与Kafka集成实践:实现高效消息队列
  • 角度回归——八参数检测四边形Gliding Vertex
  • 微服务中的 AKF 拆分原则:构建可扩展系统的核心方法论
  • 鸿蒙Flutter实战:25-混合开发详解-5-跳转Flutter页面
  • HarmonyOS学习——UIAbility组件(上)
  • 交换机工作原理解析与网络安全实践
  • 【计算机网络】TCP如何保障传输可靠性_笔记
  • C++:关联容器set容器,multiset容器
  • ss、lsof 命令
  • git:The following paths are ignored by one of your