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;
    }
};
相关推荐
野犬寒鸦3 小时前
Redis核心数据结构操作指南:字符串、哈希、列表详解
数据结构·数据库·redis·后端·缓存·哈希算法
佩奇的技术笔记3 小时前
Python入门手册:Python中的数据结构类型
数据结构·python
信息化未来5 小时前
python 生成复杂表格,自动分页等功能
开发语言·数据结构·python
星霜旅人7 小时前
【C++】红黑树
数据结构
Little-Hu8 小时前
双向链表详解
c语言·链表
姬公子5219 小时前
leetcode hot100刷题日记——21.不同路径
算法·leetcode
姬公子5219 小时前
leetcode hot100刷题日记——22.只出现一次的数字
算法·leetcode·职场和发展
心软且酷丶9 小时前
leetcode:1688. 比赛中的配对次数(python3解法,数学相关算法题)
python·算法·leetcode
緈福的街口9 小时前
【leetcode】977. 有序数组的平方
算法·leetcode·职场和发展
姬公子52111 小时前
leetcode hot100刷题日记——19.买卖股票的最佳时机
c++·算法·leetcode