LeetCode 0082.删除排序链表中的重复元素 II:模拟

【LetMeFly】82.删除排序链表中的重复元素 II:模拟

力扣题目链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/

给定一个已排序的链表的头 head删除原始链表中所有重复数字的节点,只留下不同的数字 。返回 已排序的链表

示例 1:

复制代码
输入:head = [1,2,3,3,4,4,5]
输出:[1,2,5]

示例 2:

复制代码
输入:head = [1,1,1,2,3]
输出:[2,3]

提示:

  • 链表中节点数目在范围 [0, 300]
  • -100 <= Node.val <= 100
  • 题目数据保证链表已经按升序 排列

方法一:模拟

相同的节点可能被全部删除(头节点可能也会被删),因此我们可以新建一个"空的头节点ans",ans的next指向head。

使用两个节点lastNode和thisNode,lastNode指向上一个节点(防止当前遍历到的节点被删除),thisNode指向当前处理到的节点。当thisNode和thisNode.next都非空时:

  • 如果thisNode.val == thisNode.next.val,新建一个nextNode节点指向thisNode.next.next(最终指向第一个和thisNode的值不同的节点)。当nextNode非空且nextNode.val == thisNode.val时,nextNode不断后移。最后将lastNode.next赋值为nextNode,并将thisNode赋值为nextNode(删掉了中间具有相同元素的节点)。
  • 否则,将lastNode和thisNode分别赋值为thisNode和thisNode.next(相当于指针后移)

最终返回"假头节点"ans的next即可。

  • 时间复杂度 O ( l e n ( l i s t n o d e ) ) O(len(listnode)) O(len(listnode))
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
cpp 复制代码
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* ans = new ListNode(1000, head);
        ListNode* lastNode = ans, *thisNode = head;
        while (thisNode && thisNode->next) {
            if (thisNode->val == thisNode->next->val) {
                ListNode* nextNode = thisNode->next->next;
                while (nextNode && thisNode->val == nextNode->val) {
                    nextNode = nextNode->next;
                }
                lastNode->next = nextNode;
                thisNode = nextNode;
            }
            else {
                lastNode = thisNode, thisNode = thisNode->next;
            }
        }
        return ans->next;
    }
};
Python
python 复制代码
# from typing import Optional

# # Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        ans = ListNode(1000, head)
        lastNode, thisNode = ans, head
        while thisNode and thisNode.next:
            if thisNode.val == thisNode.next.val:
                nextNode = thisNode.next.next
                while nextNode and thisNode.val == nextNode.val:
                    nextNode = nextNode.next
                lastNode.next = nextNode
                thisNode = nextNode
            else:
                lastNode, thisNode = thisNode, thisNode.next
        return ans.next

同步发文于CSDN,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/135612345

相关推荐
罗西的思考1 分钟前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
qq_433554543 小时前
C++数位DP
c++·算法·图论
AshinGau3 小时前
Softmax 与 交叉熵损失
神经网络·算法
似水এ᭄往昔3 小时前
【C++】--AVL树的认识和实现
开发语言·数据结构·c++·算法·stl
栀秋6663 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx3 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试
有意义4 小时前
斐波那契数列:从递归到优化的完整指南
javascript·算法·面试
charlie1145141914 小时前
编写INI Parser 测试完整指南 - 从零开始
开发语言·c++·笔记·学习·算法·单元测试·测试
mmz12074 小时前
前缀和问题2(c++)
c++·算法
TL滕4 小时前
从0开始学算法——第十六天(双指针算法)
数据结构·笔记·学习·算法