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

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.xdnf.cn/news/8096.html

相关文章:

  • 设计模式-工厂模式和策略模式
  • Keil5 MDK LPC1768 RT-Thread KSZ8041NL uIP1.3.1实现UDP网络通讯(服务端接收并发数据)
  • 【web应用】vue3前端框架怎么修改logo?
  • 硬件产品死机问题
  • 蓝牙低功耗(BLE)的通信信道和包类型
  • HarmonyOS 鸿蒙应用开发基础:父组件和子组件的通信方法总结
  • linux系统启动脚本(格式问题)
  • 分布式系统设计 - 性能优化
  • Spring Cloud Gateway高并发限流——基于Redis实现方案解析
  • 【LeetCode 热题 100】二分查找 系列
  • 多维应用场景的落地实践的智慧园区开源了
  • HarmonyOS优化应用文件上传下载慢问题性能优化二
  • MVC 与 MVT:Web 开发架构模式的异同与实践
  • spark-Catalyst 优化器和 Tungsten 执行引擎介绍
  • AI之光,点亮星途 :揭秘“智语心桥”,如何用科技为孤独症儿童架起沟通的桥梁
  • (第95天)OGG 微服务搭建 Oracle 19C 到 MySQL 8 双向同步
  • 可信计算是什么?可信逻辑:计算系统安全的形式化分析框架
  • 【brpc】安装与使用
  • AGI大模型(32):LangChain实现RAG
  • NSSCTF-[陇剑杯 2021]webshell(问6)
  • 关于如何在Springboot项目中通过excel批量导入数据
  • Flask vs. Django:如何选择最适合你的 Web 框架?
  • 基于Scikit-learn与Flask的医疗AI糖尿病预测系统开发实战
  • 蓝桥杯 3. 涂色
  • OceanBase数据库全面指南(基础入门篇)
  • C# 实现轻量化数据库SQLite在工业中上的应用
  • TensorFlow深度学习实战(17)——主成分分析详解
  • 鞋服行业数据防泄露——企业解决方案
  • NFS服务器实验
  • 深入了解linux系统—— 文件系统