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;
    }
};
相关推荐
小徐不徐说22 分钟前
C++ 模板与 STL 基础入门:从泛型编程到实战工具集
开发语言·数据结构·c++·qt·面试
Tisfy2 小时前
LeetCode 2411.按位或最大的最小子数组长度:一次倒序遍历
数据结构·算法·leetcode·题解·位运算·遍历
晚云与城3 小时前
【数据结构】-----排序的艺术画卷
数据结构·算法·排序算法
j_xxx404_3 小时前
数据结构:算法复杂度与空间复杂度
c语言·数据结构·算法
m0_749317524 小时前
力扣-字母异位词
java·算法·leetcode·职场和发展
墨染点香4 小时前
LeetCode 刷题【24. 两两交换链表中的节点、25. K 个一组翻转链表】
算法·leetcode·链表
自由随风飘5 小时前
旅游城市数量最大化 01背包问题
数据结构·c++·算法·动态规划·旅游
YuTaoShao5 小时前
【LeetCode 热题 100】155. 最小栈
java·算法·leetcode
好好先森&6 小时前
C语言:冒泡排序
c语言·数据结构·算法·遍历·冒牌排序
肉夹馍不加青椒6 小时前
第二十三天(数据结构:链表补充【希尔表】)
数据结构·链表