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

相关推荐
Eric.Lee202126 分钟前
数据集-目标检测系列- 螃蟹 检测数据集 crab >> DataBall
python·深度学习·算法·目标检测·计算机视觉·数据集·螃蟹检测
林辞忧35 分钟前
算法修炼之路之滑动窗口
算法
￴ㅤ￴￴ㅤ9527超级帅1 小时前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
liuyang-neu1 小时前
力扣 简单 110.平衡二叉树
java·算法·leetcode·深度优先
一个不知名程序员www1 小时前
leetcode面试题17.04:消失的数字(C语言版)
leetcode
penguin_bark1 小时前
LCR 068. 搜索插入位置
算法·leetcode·职场和发展
_GR1 小时前
每日OJ题_牛客_牛牛冲钻五_模拟_C++_Java
java·数据结构·c++·算法·动态规划
ROBIN__dyc1 小时前
表达式
算法
无限大.1 小时前
c语言实例
c语言·数据结构·算法
六点半8882 小时前
【C++】速通涉及 “vector” 的经典OJ编程题
开发语言·c++·算法·青少年编程·推荐算法