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;
}
相关推荐
释厄6235 小时前
学术引用本体论——WorkBuddy 学术科学文化三违反
网络·人工智能·算法
可乐鸡翅yeah_6 小时前
hls.js 缓冲区参数 maxBufferLength、maxBufferSize 通俗讲解与业务调参
开发语言·javascript·ios·音视频·safari·m3u8
梦帮科技6 小时前
【3.0修订版】 RNS 代币架构:ERC20 五件套扩展与六钱包分配
数据结构·后端·算法·架构·node.js·区块链·php
Wang's Blog6 小时前
Java框架 SpringCloud 快速入门: 全局过滤器(GlobalFilter)自定义鉴权
java·开发语言·spring cloud
白山编程大哥6 小时前
C语言篇:可变参数函数
c语言·开发语言
水饺编程6 小时前
第1章:开发环境搭建,在 Windows 中安装 VS 2019
linux·c语言·汇编·visual studio
(Charon)6 小时前
【C++面试】程序崩溃如何定位:从Segmentation fault到GDB、Core Dump与ASan
开发语言·c++
“AI国潮设计-小江”6 小时前
《Python+SDXL实战:用ControlNet精准控制“英歌舞戚风蛋糕”质感,附批量生成脚本》
开发语言·人工智能·python·prompt·aigc
薛定e的猫咪6 小时前
(AISTATS 2023)BaCaDI:未知干预下的贝叶斯因果发现
人工智能·深度学习·算法·机器学习
1024奇点6 小时前
【仓颉语言入门 · 第26课】
开发语言·ide·开源