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

[复制链接]
发表于 2025-7-30 08:01:22 | 显示全部楼层 |阅读模式
目次
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


代码
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     ListNode *next;
  6. *     ListNode() : val(0), next(nullptr) {}
  7. *     ListNode(int x) : val(x), next(nullptr) {}
  8. *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13.     ListNode* deleteDuplicates(ListNode* head) {
  14.         if(head == nullptr)
  15.             return head;
  16.         ListNode* pre = head;
  17.         ListNode* cur = head->next;
  18.         ListNode* nex = nullptr;
  19.         while(cur){
  20.             nex = cur->next;
  21.             if(cur->val == pre->val){
  22.                 pre->next = nex;
  23.                 delete cur;
  24.                 cur = nex;
  25.             }else{
  26.                 pre = cur;
  27.                 cur = nex;
  28.             }
  29.         }
  30.         return head;
  31.     }
  32. };
复制代码
82. Remove Duplicates from Sorted List II

 
代码
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     ListNode *next;
  6. *     ListNode() : val(0), next(nullptr) {}
  7. *     ListNode(int x) : val(x), next(nullptr) {}
  8. *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13.     ListNode* deleteDuplicates(ListNode* head) {
  14.         if(head == nullptr)
  15.             return head;
  16.         ListNode* dummy = new ListNode(-1,head);
  17.         ListNode* prepre = dummy;
  18.         int pre_val = head->val;
  19.         ListNode* pre = head;
  20.         ListNode* cur = pre->next;
  21.         ListNode* nex = nullptr;
  22.         while(cur){
  23.             nex = cur->next;
  24.             if(cur->val == pre_val){
  25.                 prepre->next = nex;
  26.                 if(pre){
  27.                     delete pre;
  28.                     pre = nullptr;
  29.                 }
  30.                 delete cur;
  31.                 cur = nex;
  32.             }else{
  33.                 if(pre){
  34.                     prepre = pre;
  35.                 }
  36.                 pre = cur;
  37.                 pre_val = pre->val;
  38.                 cur = nex;
  39.             }
  40.         }
  41.         ListNode* ans = dummy->next;
  42.         delete dummy;
  43.         return ans;
  44.     }
  45. };
复制代码

1836. Remove Duplicates From an Unsorted Linked List

 


代码:
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     ListNode *next;
  6. *     ListNode() : val(0), next(nullptr) {}
  7. *     ListNode(int x) : val(x), next(nullptr) {}
  8. *     ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13.     ListNode* deleteDuplicatesUnsorted(ListNode* head) {
  14.         unordered_map<int,int> count;
  15.         ListNode* cur = head;
  16.         while(cur){
  17.             count[cur->val]++;
  18.             cur = cur->next;
  19.         }
  20.         ListNode* dummy = new ListNode(-1,head);
  21.         ListNode* pre = dummy;
  22.         cur = head;
  23.         ListNode* post = nullptr;
  24.         while(cur){
  25.             post = cur->next;
  26.             if(count[cur->val] > 1){
  27.                 pre->next = post;
  28.                 // delete cur;
  29.                 cur = post;
  30.             }else{
  31.                 pre = cur;
  32.                 cur = post;
  33.             }
  34.         }
  35.         ListNode* ans = dummy->next;
  36.         delete dummy;
  37.         return ans;
  38.     }
  39. };
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表