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

相关推荐
lingxiao1688819 分钟前
双目立体视觉
图像处理·算法·机器学习·计算机视觉
JNU freshman23 分钟前
和为target问题汇总
算法
2401_8590490827 分钟前
MSPM0--Timer(一口一口喂版)
arm开发·单片机·mcu·算法
寂空_32 分钟前
【算法笔记】ACM数论基础模板
c++·笔记·算法
ggabb41 分钟前
当九九乘法口诀“出海”英国:文化碰撞下的数学教育变革
算法
爱coding的橙子1 小时前
每日算法刷题计划Day7 5.15:leetcode滑动窗口4道题,用时1h
算法·leetcode
wuqingshun3141591 小时前
蓝桥杯 10. 全球变暖
c++·算法·职场和发展·蓝桥杯
阳洞洞1 小时前
leetcode 56. 合并区间
leetcode
手握风云-1 小时前
二叉树深搜:在算法森林中寻找路径
算法
xu_wenming1 小时前
华为Watch的ECG功能技术分析
人工智能·嵌入式硬件·算法