鱼C论坛

 找回密码
 立即注册
查看: 979|回复: 0

[技术交流] 【朱迪的LeetCode刷题笔记】82. Remove Duplicates from Sorted List II #Medium #C

[复制链接]
发表于 2021-4-7 03:45:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

  1. 82. Remove Duplicates from Sorted List II #Medium

  2. 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.


  3. Example 1:

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


  6. Example 2:

  7. Input: head = [1,1,1,2,3]
  8. Output: [2,3]


  9. Constraints:

  10. The number of nodes in the list is in the range [0, 300].
  11. -100 <= Node.val <= 100
  12. The list is guaranteed to be sorted in ascending order.
复制代码


C

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     struct ListNode *next;
  6. * };
  7. */


  8. struct ListNode* deleteDuplicates(struct ListNode* head) {
  9.     struct ListNode *before = NULL;
  10.     struct ListNode *current = head;
  11.     if (head == NULL) {
  12.         return NULL;
  13.     }
  14.     struct ListNode *next = head->next;
  15.     while (next) {
  16.         if (current->val == next->val) {
  17.             while (next && current->val == next->val) {
  18.                 current = current->next;
  19.                 next = next->next;
  20.             }
  21.             if (next == NULL) {
  22.                 if (before == NULL) {
  23.                     return NULL;
  24.                 }
  25.                 before->next = NULL;
  26.                 return head;
  27.             }
  28.             current = current->next;
  29.             next = next->next;
  30.             if (before == NULL) {
  31.                 head = current;
  32.             } else {
  33.                 before->next = current;
  34.             }
  35.         } else {
  36.             before = current;
  37.             current = current->next;
  38.             next = next->next;
  39.         }     
  40.     }
  41.     return head;
  42. }
复制代码


不愧是medium 想了三天qwq
unknown.png

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-21 15:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表