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

相关推荐
程序猿Eason35 分钟前
U587038 背包 题解
c++·算法·动态规划
potato_may1 小时前
第18讲:C语言内存函数
c语言·数据结构·算法
dingzd952 小时前
TikTok推荐算法快速解析
算法·机器学习·web3·facebook·推荐算法·tiktok·instagram
仰泳的熊猫2 小时前
LeetCode:95. 不同的二叉搜索树 II
数据结构·c++·算法·leetcode
Nix Lockhart2 小时前
《算法与数据结构》第七章[算法4]:最短路径
c语言·数据结构·学习·算法·图论
前进之路92 小时前
Leetcode每日一练--28
leetcode
用户6605307619622 小时前
UFlow:像素级工业零件异常检测 Normalized Flow 方法
算法
爱吃烤鸡翅的酸菜鱼2 小时前
从零掌握贪心算法Java版:LeetCode 10题实战解析(上)
java·算法
workflower3 小时前
软件工程的知识领域
开发语言·算法·软件工程·个人开发·结对编程
budingxiaomoli3 小时前
算法--双指针二
算法