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

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.lryc.cn/news/2386975.html

相关文章:

  • 每日leetcode(昨天赶飞机没做,今天补)
  • SDL2常用函数:SDL_BlitSurfaceSDL_UpdateWindowSurface 数据结构及使用介绍
  • 【LeetCode 热题 100】买卖股票的最佳时机 / 跳跃游戏 / 划分字母区间
  • 万亿参数背后的算力密码:大模型训练的分布式架构与自动化运维全解析
  • LangChain03-图数据库与LangGraph
  • rabbitmq单机多实例部署
  • Linux10正式版发布,拥抱AI了!
  • 在离线 OpenEuler-22.03 服务器上升级 OpenSSH 的完整指南
  • 全能邮箱全能邮箱:实现邮件管理的自动化!
  • [特殊字符] Linux 日志查看与分析常用命令全攻略
  • mysql-tpcc-mysql压测工具使用
  • Qt找不到windows API报错:error: LNK2019: 无法解析的外部符号 __imp_OpenClipboard
  • 机试 | vector/array Minimum Glutton C++
  • OpenCv高阶(十七)——dlib库安装、dlib人脸检测
  • 前端内容黑白处理、轮播图、奇妙的头像特效
  • 蓝桥杯 10. 安全序列
  • (10)-java+ selenium->元素之By class name
  • Git - .gitignore 文件
  • MPI与多线程(如OpenMP)混合编程注意事项与性能优化
  • 计算机网络学习(八)——MAC
  • 英语六级-阅读篇
  • 右键打开 pycharm 右键 pycharm
  • 机器人坐标系标定
  • Flink流处理基础概论
  • 【RabbitMQ】记录 InvalidDefinitionException: Java 8 date/time type
  • 如何通过API接口实现自动化上货跨平台铺货?商品采集|商品上传实现详细步骤
  • 《三维点如何映射到图像像素?——相机投影模型详解》
  • Go 语言范围循环变量重用问题与 VSCode 调试解决方法
  • 青少年编程与数学 02-020 C#程序设计基础 04课题、常量和变量
  • 零基础设计模式——结构型模式 - 适配器模式