|
| 1 | +--- |
| 2 | +title: 82.删除排序链表中的重复元素 II |
| 3 | +date: 2024-01-15 21:42:35 |
| 4 | +tags: [题解, LeetCode, 中等, 链表, 双指针] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】82.删除排序链表中的重复元素 II:模拟 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/](https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/) |
| 10 | + |
| 11 | +<p>给定一个已排序的链表的头 <code>head</code> , <em>删除原始链表中所有重复数字的节点,只留下不同的数字</em> 。返回 <em>已排序的链表</em> 。</p> |
| 12 | + |
| 13 | +<p> </p> |
| 14 | + |
| 15 | +<p><strong>示例 1:</strong></p> |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg" style="height: 142px; width: 500px;" /> |
| 17 | +<pre> |
| 18 | +<strong>输入:</strong>head = [1,2,3,3,4,4,5] |
| 19 | +<strong>输出:</strong>[1,2,5] |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong>示例 2:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg" style="height: 164px; width: 400px;" /> |
| 24 | +<pre> |
| 25 | +<strong>输入:</strong>head = [1,1,1,2,3] |
| 26 | +<strong>输出:</strong>[2,3] |
| 27 | +</pre> |
| 28 | + |
| 29 | +<p> </p> |
| 30 | + |
| 31 | +<p><strong>提示:</strong></p> |
| 32 | + |
| 33 | +<ul> |
| 34 | + <li>链表中节点数目在范围 <code>[0, 300]</code> 内</li> |
| 35 | + <li><code>-100 <= Node.val <= 100</code></li> |
| 36 | + <li>题目数据保证链表已经按升序 <strong>排列</strong></li> |
| 37 | +</ul> |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +## 方法一:模拟 |
| 42 | + |
| 43 | +相同的节点可能被全部删除(头节点可能也会被删),因此我们可以新建一个“空的头节点ans”,ans的next指向head。 |
| 44 | + |
| 45 | +使用两个节点lastNode和thisNode,lastNode指向上一个节点(防止当前遍历到的节点被删除),thisNode指向当前处理到的节点。当thisNode和thisNode.next都非空时: |
| 46 | + |
| 47 | ++ 如果```thisNode.val == thisNode.next.val```,新建一个nextNode节点指向thisNode.next.next(最终指向第一个和thisNode的值不同的节点)。当nextNode非空且```nextNode.val == thisNode.val```时,nextNode不断后移。最后将lastNode.next赋值为nextNode,并将thisNode赋值为nextNode(删掉了中间具有相同元素的节点)。 |
| 48 | ++ 否则,将lastNode和thisNode分别赋值为thisNode和thisNode.next(相当于指针后移) |
| 49 | + |
| 50 | +最终返回“假头节点”ans的next即可。 |
| 51 | + |
| 52 | ++ 时间复杂度$O(len(listnode))$ |
| 53 | ++ 空间复杂度$O(1)$ |
| 54 | + |
| 55 | +### AC代码 |
| 56 | + |
| 57 | +#### C++ |
| 58 | + |
| 59 | +```cpp |
| 60 | +class Solution { |
| 61 | +public: |
| 62 | + ListNode* deleteDuplicates(ListNode* head) { |
| 63 | + ListNode* ans = new ListNode(1000, head); |
| 64 | + ListNode* lastNode = ans, *thisNode = head; |
| 65 | + while (thisNode && thisNode->next) { |
| 66 | + if (thisNode->val == thisNode->next->val) { |
| 67 | + ListNode* nextNode = thisNode->next->next; |
| 68 | + while (nextNode && thisNode->val == nextNode->val) { |
| 69 | + nextNode = nextNode->next; |
| 70 | + } |
| 71 | + lastNode->next = nextNode; |
| 72 | + thisNode = nextNode; |
| 73 | + } |
| 74 | + else { |
| 75 | + lastNode = thisNode, thisNode = thisNode->next; |
| 76 | + } |
| 77 | + } |
| 78 | + return ans->next; |
| 79 | + } |
| 80 | +}; |
| 81 | +``` |
| 82 | +
|
| 83 | +#### Python |
| 84 | +
|
| 85 | +```python |
| 86 | +# from typing import Optional |
| 87 | +
|
| 88 | +# # Definition for singly-linked list. |
| 89 | +# class ListNode: |
| 90 | +# def __init__(self, val=0, next=None): |
| 91 | +# self.val = val |
| 92 | +# self.next = next |
| 93 | +
|
| 94 | +class Solution: |
| 95 | + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: |
| 96 | + ans = ListNode(1000, head) |
| 97 | + lastNode, thisNode = ans, head |
| 98 | + while thisNode and thisNode.next: |
| 99 | + if thisNode.val == thisNode.next.val: |
| 100 | + nextNode = thisNode.next.next |
| 101 | + while nextNode and thisNode.val == nextNode.val: |
| 102 | + nextNode = nextNode.next |
| 103 | + lastNode.next = nextNode |
| 104 | + thisNode = nextNode |
| 105 | + else: |
| 106 | + lastNode, thisNode = thisNode, thisNode.next |
| 107 | + return ans.next |
| 108 | +``` |
| 109 | + |
| 110 | +> 同步发文于CSDN,原创不易,转载经作者同意后请附上[原文链接](https://blog.tisfy.eu.org/2024/01/15/LeetCode%200082.%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0II/)哦~ |
| 111 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/135612345](https://letmefly.blog.csdn.net/article/details/135612345) |
0 commit comments