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:
c 复制代码
/**
 * 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;
}
相关推荐
世洋Blog3 分钟前
算法导论-分治法和合并(Merge)排序
算法
源代码•宸9 分钟前
Golang基础语法(go语言结构体、go语言数组与切片、go语言条件句、go语言循环)
开发语言·经验分享·后端·算法·golang·go
l1t15 分钟前
快速加载CSV文件到数据库的工具pg_csv_loader
数据库·算法
ベadvance courageouslyミ21 分钟前
项目一(线程邮箱)
c语言·线程·makefile·进程间通信·线程邮箱
yugi98783822 分钟前
基于Matlab的晴空指数计算实现
开发语言·算法·matlab
ADI_OP39 分钟前
ADAU1452的开发教程3:常规音频算法的开发(2)
算法·dsp开发·adi dsp中文资料·adi dsp·adi音频dsp·adi dsp开发教程
666HZ66640 分钟前
数据结构1.0 数据结构在学什么
数据结构·算法
君义_noip1 小时前
信息学奥赛一本通 1951:【10NOIP普及组】导弹拦截 | 洛谷 P1158 [NOIP 2010 普及组] 导弹拦截
c++·算法·csp-j·信息学奥赛
环黄金线HHJX.1 小时前
《QuantumTuan ⇆ QT:Qt》
人工智能·qt·算法·编辑器·量子计算
jz_ddk1 小时前
[实战] 射频相位噪声单位转换:从dBc/Hz到rad
算法·rf·射频·相位噪声·相噪