LeetCode //C - 83. Remove Duplicates from Sorted List

83. Remove Duplicates from Sorted List

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

Example 1:

Input: head = 1,1,2
Output: 1,2

Example 2:

Input: head = 1,1,2,3,3
Output: 1,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: 83. Remove Duplicates from Sorted List


Solution:

Ideas:
  1. Check for an Empty List: Before beginning any operations on the linked list, the code checks if the head pointer is NULL. If it is, the function immediately returns NULL, indicating that the list is empty and there are no nodes to process.

  2. Traversal with Current Pointer: The function uses a pointer called current to traverse the linked list. This pointer starts at the head of the list.

  3. Duplicate Removal Logic: As the list is sorted, all duplicates of an element are adjacent. The current pointer checks the current node (current) against the next node (current->next). If both nodes have the same value (current->val == current->next->val), it means a duplicate is found.

  4. Removing the Duplicate Node:

  • To remove the duplicate, the link from the current node to the next node is redirected to skip one node, effectively removing the next node from the list (current->next = current->next->next).
  • The removed node is then freed using free(), assuming memory management functions are available. This is crucial to prevent memory leaks.
  1. Advancement: If no duplicate is found, the current pointer simply moves to the next node in the list (current = current->next).

  2. Continuation Until the End: This process is repeated until current or current->next becomes NULL, indicating that the end of the list has been reached or there are no more nodes to compare.

  3. Return the Modified List: Finally, the function returns the modified list starting from head. All duplicates have been removed, and the list remains sorted.

Code:
c 复制代码
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if (head == NULL) return NULL;

    struct ListNode* current = head;
    while (current->next != NULL) {
        if (current->val == current->next->val) {
            // Remove the duplicate node
            struct ListNode* temp = current->next;
            current->next = temp->next;
            free(temp); // Assuming stdlib.h is included for free()
        } else {
            current = current->next; // Move to the next element if no duplicate
        }
    }
    return head;
}
相关推荐
leisoo809722 分钟前
财报数据怎么排雷本地化Python构建财务异常预警系统
人工智能·python·算法
zlinear数据采集卡34 分钟前
D223上位机C#开发实战:从帧解析到波形显示的完整实现
开发语言·arm开发·嵌入式硬件·fpga开发·开源·c#
Poo_Chai1 小时前
QT emit信号后完整处理流程,包括槽函数响应流程
java·开发语言·数据库
瑞码空间1 小时前
Java 图形界面(GUI)完整知识点手册
java·开发语言·图形界面·swing
小努蛋2 小时前
linux编译openresty异常问题,lua_cjson.c:706:5: 错误: ‘for‘ 循环初始化声明只在 C99 或 C++ 模式下允许
开发语言·lua·openresty
qz_Serene2 小时前
C++:类和对象(上)
开发语言·c++
西安景驰电子2 小时前
《PTP精确时间协议系列》第一篇:从原理到应用,全面解读IEEE 1588
linux·运维·服务器·开发语言·网络·windows·php
ctlover2 小时前
Python文件操作
开发语言·python
luj_17682 小时前
塔防牌:策略与卡牌的智慧碰撞
服务器·c语言·开发语言·经验分享·算法
霸道流氓气质3 小时前
基于本地 PaddleOCR 的 PDF 文字识别完整技术文档
开发语言·r语言·pdf