• LeetCode //C - 82. Remove Duplicates from Sorted List II


    82. Remove Duplicates from Sorted List II

    Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.
     

    Example 1:

    在这里插入图片描述

    Input: head = [1,2,3,3,4,4,5]
    Output: [1,2,5]

    Example 2:

    在这里插入图片描述

    Input: head = [1,1,1,2,3]
    Output: [2,3]

    Constraints:
    • The number of nodes in the list is in the range [0, 300].
    • -100 <= Node.val <= 100
    • The list is guaranteed to be sorted in ascending order.

    From: LeetCode
    Link: 82. Remove Duplicates from Sorted List II


    Solution:

    Ideas:

    1. Initialize Pointers and Dummy Node:

    • Create a dummy node that acts as a placeholder for the new list. Initialize prev to this dummy node and curr to the head of the original list.

    2. Traverse the Original List:

    • Go through each node in the list using the curr pointer.

    3. Check for Duplicates:

    • If the curr node has a duplicate (i.e., curr->val is the same as curr->next->val), move curr to the last duplicate node. This is done using the while loop within the main while loop.

    4. Add Unique Nodes to New List:

    • If the curr node is not a duplicate, add it to the new list. The prev pointer keeps track of the last node added to the new list.

    5. Move to Next Node:

    • Move curr to the next node in the original list.

    6. Terminate New List:

    • Make sure to set the next of the last node in the new list to NULL.

    7. Return New List:

    • The new list starts after the dummy node. So, we return dummy->next as the new head of the list.
    Code:
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* deleteDuplicates(struct ListNode* head) {
        // Create a dummy node to serve as the head of the new list
        struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
        dummy->val = 0;
        dummy->next = NULL;
        
        // Initialize pointers
        struct ListNode* prev = dummy;
        struct ListNode* curr = head;
        
        while (curr != NULL) {
            // Check if the current node is a duplicate
            int duplicate = 0;
            while (curr->next != NULL && curr->val == curr->next->val) {
                duplicate = 1;
                curr = curr->next;
            }
            
            // If it's not a duplicate, add it to the new list
            if (duplicate == 0) {
                prev->next = curr;
                prev = curr;
            }
            
            // Move to the next node in the original list
            curr = curr->next;
        }
        
        // Terminate the new list
        prev->next = NULL;
        
        // Return the new list (skipping the dummy node)
        struct ListNode* newHead = dummy->next;
        free(dummy);
        return newHead;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    MySql Workbench 8.0汉化插件分享
    开仓风险计算器.xlsx(可计算:名义价值、最大资金亏损、开仓所需保证金、开仓资金杠杆、最小逐仓保证金、U本位需开张数、币本位需开张数)
    Netty
    面试官常问到的问题
    网络通信基本原理
    表白墙网站练习【前端+后端+数据库】
    Linux中的开发工具(yum,vim,gcc/g++,gdb,Makefile,git)
    项目保密协议书(范本)
    【FOC控制】英飞凌TC264无刷驱动方案simplefoc移植(5)-磁编码器移植AS5600 软件IIC
    艾美捷nickases内切酶活性检测及相关研究
  • 原文地址:https://blog.csdn.net/navicheung/article/details/132574621