链表 删除单链表的重复节点

力扣链接

  • 题目描述
    编写代码,移除未排序链表中的重复节点。保留最开始出现的节点。
    示例1: 输入:[1, 2, 3, 3, 2, 1]
    输出:[1, 2, 3]
    示例2: 输入:[1, 1, 1, 1, 2]
    输出:[1, 2]
  • 提示: 链表长度在[0, 20000]范围内。 链表元素在[0, 20000]范围内。
  • 进阶:
    如果不得使用临时缓冲区,该怎么解决?
  • 解题思路
  • 思路一:定义两个指针current和p来逐个遍历链表,current元素依次和p比较,直到p为NULL,current向后移动一个。
  • 代码实现
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
/*
函数主要是两层循环,外层循环实现遍历整个链表,内层循环负责遍历比较当前节点值与其后所有节点值,若相等则跳过,
*/
struct ListNode* removeDuplicateNodes(struct ListNode* head) {
    // Return NULL if the list is empty
    if (head == NULL) return NULL;
    
    struct ListNode *current = head;

    // Iterate through each node in the list
    while (current) {
        struct ListNode* p = current;
        // Check subsequent nodes for duplicates
        while (p->next) {
            // If a duplicate is found, remove it
            if (p->next->val == current->val) {
                p->next = p->next->next;
            } else {
                // Otherwise, move to the next node
                p = p->next;
            }
        }
        // Move to the next node in the list
        current = current->next;
    }
    
    // Return the modified list
    return head;
}

  • 问题记录
  1. 内层循环的结果是在哪保存的?
    内层循环遍历current节点后的所有节点,发现有重复值时,就会改变节点的指向,即已经改变了链表结构。下一次遍历时,链表已经是改变后的链表了。
  • 思路二:利用标记数组,标记当前值是否出现过。
struct ListNode* removeDuplicateNodes(struct ListNode* head){
       if(head == NULL || head->next == NULL) return NULL;

       int index[20001] = {0};
       index[head->val] = 1;

        struct ListNode *pre = head, *q = head->next;
       while(q){
            if(index[q->val] == 0){
                index[q->val] = 1;
                pre = q;
                q = q->next;
            }else{
                pre->next = q->next;
                q = pre->next;
            }
       }
       return head;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值