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

目录

[83. Remove Duplicates from Sorted List](#83. Remove Duplicates from Sorted List)

[82. Remove Duplicates from Sorted List II](#82. Remove Duplicates from Sorted List II)

[1836. Remove Duplicates From an Unsorted Linked List](#1836. Remove Duplicates From an Unsorted Linked List)


删除链表中的结点合集

83. Remove Duplicates from Sorted List

代码:

cpp 复制代码
/**
 * 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

代码:

cpp 复制代码
/**
 * 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

代码:

cpp 复制代码
/**
 * 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;
    }
};
相关推荐
季明洵几秒前
二分搜索、移除元素、有序数组的平方、长度最小的子数组
java·数据结构·算法·leetcode
Sheep Shaun2 分钟前
深入理解AVL树:从概念到完整C++实现详解
服务器·开发语言·数据结构·c++·后端·算法
YuTaoShao3 分钟前
【LeetCode 每日一题】3314. 构造最小位运算数组 I —— (解法二)
算法·leetcode·职场和发展
大猫子的技术日记6 分钟前
Redis 快速上手:5 分钟掌握核心能力
数据结构·数据库·redis·缓存·持久化·pub/sub
云深麋鹿7 分钟前
二.顺序表和链表
c语言·开发语言·数据结构·链表
Tisfy19 分钟前
LeetCode 3507.移除最小数对使数组有序 I:纯模拟
算法·leetcode·题解·模拟·数组
努力学算法的蒟蒻22 分钟前
day63(1.22)——leetcode面试经典150
算法·leetcode·面试
苦藤新鸡41 分钟前
27.合并有序链表,串葫芦
前端·javascript·链表
程序员-King.43 分钟前
day161—动态规划—最长递增子序列(LeetCode-300)
算法·leetcode·深度优先·动态规划·递归
历程里程碑1 小时前
Linux 2 指令(2)进阶:内置与外置命令解析
linux·运维·服务器·c语言·开发语言·数据结构·ubuntu