当前位置: 首页 > 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;}
};

 

相关文章:

  • 微服务的应用案例
  • 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
  • 单例模式的运用
  • 企业简介的网站怎么做/广告软文小故事800字
  • 网站开发试验报告/广告资源网
  • 网站如何有排名靠前/2345网址中国最好
  • 工程公司名称大全集最新免费/大连做优化网站哪家好
  • dw代码做网站/百度seo详解
  • 重庆南川网站制作公司推荐/网络推广公司口碑