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

相关推荐
martian66516 分钟前
支持向量机(SVM)深度解析:从数学根基到工程实践
算法·机器学习·支持向量机
孟大本事要学习21 分钟前
算法19天|回溯算法:理论基础、组合、组合总和Ⅲ、电话号码的字母组合
算法
??tobenewyorker1 小时前
力扣打卡第二十一天 中后遍历+中前遍历 构造二叉树
数据结构·c++·算法·leetcode
贾全1 小时前
第十章:HIL-SERL 真实机器人训练实战
人工智能·深度学习·算法·机器学习·机器人
GIS小天2 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
满分观察网友z2 小时前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
森焱森3 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天3 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid
闪电麦坤954 小时前
数据结构:二维数组(2D Arrays)
数据结构·算法
凌肖战4 小时前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode