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;
}
相关推荐
Σίσυφος19006 分钟前
RANSAC算法原理与应用
算法
我星期八休息21 分钟前
MySQL数据可视化实战指南
数据库·人工智能·mysql·算法·信息可视化
程序员-King.29 分钟前
day144—递归—平衡二叉树(LeetCode-110)
算法·leetcode·二叉树·递归
老鼠只爱大米31 分钟前
LeetCode经典算法面试题 #739:每日温度(单调栈、动态规划等多种实现方案详解)
算法·leetcode·面试·动态规划·单调栈·每日温度
老鼠只爱大米39 分钟前
LeetCode经典算法面试题 #394:字符串解码(递归、双栈、迭代构建等五种实现方案详解)
算法·leetcode·面试·递归··字符串解码
独自破碎E1 小时前
【回溯+剪枝】字符串的排列
算法·机器学习·剪枝
Smart-佀1 小时前
FPGA入门:CAN总线原理与Verilog代码详解
单片机·嵌入式硬件·物联网·算法·fpga开发
漫随流水1 小时前
leetcode算法(513.找树左下角的值)
数据结构·算法·leetcode·二叉树
囊中之锥.2 小时前
机器学习算法详解:DBSCAN 聚类原理、实现流程与优缺点分析
算法·机器学习·聚类
一路往蓝-Anbo2 小时前
【第42期】调试进阶(一):IDE中的Register与Memory窗口
c语言·开发语言·ide·stm32·单片机·嵌入式硬件