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壹1 小时前
单链表:数据结构中的高效指针艺术
c语言·开发语言·数据结构
耳总是一颗苹果4 小时前
排序---插入排序
数据结构·算法·排序算法
YLCHUP4 小时前
【联通分量】题解:P13823 「Diligent-OI R2 C」所谓伊人_连通分量_最短路_01bfs_图论_C++算法竞赛
c语言·数据结构·c++·算法·图论·广度优先·图搜索算法
晴空闲雲5 小时前
数据结构与算法-字符串、数组和广义表(String Array List)
数据结构·算法
Dovis(誓平步青云)6 小时前
《C++哈希表:高效数据存储与检索的核心技术》
数据结构·散列表·哈希表
秋难降7 小时前
聊聊 “摸鱼式” 遍历 —— 受控遍历的小心机
数据结构·算法·程序员
Code_Artist8 小时前
[Java并发编程]4.阻塞队列
java·数据结构·后端
嗑嗑嗑瓜子的猫8 小时前
大白话聊一聊,数据结构的基石:数组和链表
数据结构·链表·数组
月盈缺11 小时前
学习嵌入式的第二十五天——哈希表和内核链表
学习·链表·散列表
小xin过拟合11 小时前
day20 二叉树part7
开发语言·数据结构·c++·笔记·算法