Skip to content

Commit 1eea7a2

Browse files
authored
Merge pull request #188 from LetMeFly666/82
添加了问题“82.删除排序链表中的重复元素II”的代码和题解
2 parents e8d64ec + 2539a13 commit 1eea7a2

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-01-15 21:33:23
4+
* @LastEditors: LetMeFly
5+
* @LastEditTime: 2024-01-15 21:43:18
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
ListNode* deleteDuplicates(ListNode* head) {
14+
ListNode* ans = new ListNode(1000, head);
15+
ListNode* lastNode = ans, *thisNode = head;
16+
while (thisNode && thisNode->next) {
17+
if (thisNode->val == thisNode->next->val) {
18+
ListNode* nextNode = thisNode->next->next;
19+
while (nextNode && thisNode->val == nextNode->val) {
20+
nextNode = nextNode->next;
21+
}
22+
lastNode->next = nextNode;
23+
thisNode = nextNode;
24+
}
25+
else {
26+
lastNode = thisNode, thisNode = thisNode->next;
27+
}
28+
}
29+
return ans->next;
30+
}
31+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-01-15 21:45:39
4+
LastEditors: LetMeFly
5+
LastEditTime: 2024-01-15 21:53:04
6+
'''
7+
from typing import Optional
8+
9+
# Definition for singly-linked list.
10+
class ListNode:
11+
def __init__(self, val=0, next=None):
12+
self.val = val
13+
self.next = next
14+
15+
class Solution:
16+
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
17+
ans = ListNode(1000, head)
18+
lastNode, thisNode = ans, head
19+
while thisNode and thisNode.next:
20+
if thisNode.val == thisNode.next.val:
21+
nextNode = thisNode.next.next
22+
while nextNode and thisNode.val == nextNode.val:
23+
nextNode = nextNode.next
24+
lastNode.next = nextNode
25+
thisNode = nextNode
26+
else:
27+
lastNode, thisNode = thisNode, thisNode.next
28+
return ans.next

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
|0062.不同路径|中等|<a href="https://leetcode.cn/problems/unique-paths/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/11/26/LeetCode%200062.%E4%B8%8D%E5%90%8C%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/128059055" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/unique-paths/solutions/1992721/letmefly-62bu-tong-lu-jing-liang-chong-f-e8tl/" target="_blank">LeetCode题解</a>|
5555
|0067.二进制求和|简单|<a href="https://leetcode.cn/problems/add-binary/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/07/14/LeetCode%200067.%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%B1%82%E5%92%8C/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125793685" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/add-binary/solution/by-tisfy-o5z5/" target="_blank">LeetCode题解</a>|
5656
|0070.爬楼梯|简单|<a href="https://leetcode.cn/problems/climbing-stairs/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/12/10/LeetCode%200070.%E7%88%AC%E6%A5%BC%E6%A2%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134913892" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/climbing-stairs/solutions/2561558/letmefly-70pa-lou-ti-dong-tai-gui-hua-di-7d8m/" target="_blank">LeetCode题解</a>|
57+
|0082.删除排序链表中的重复元素II|中等|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/solutions/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135612345" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list-ii/solutions/2605736/letmefly-82shan-chu-pai-xu-lian-biao-zho-92xm/" target="_blank">LeetCode题解</a>|
5758
|0083.删除排序链表中的重复元素|简单|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2024/01/14/LeetCode%200083.%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%A0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/135581000" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/remove-duplicates-from-sorted-list/solutions/2603584/letmefly-83shan-chu-pai-xu-lian-biao-zho-cey8/" target="_blank">LeetCode题解</a>|
5859
|0086.分隔链表|中等|<a href="https://leetcode.cn/problems/partition-list/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2022/06/26/LeetCode%200086.%E5%88%86%E9%9A%94%E9%93%BE%E8%A1%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125467594" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/partition-list/solution/letmefly-86fen-ge-lian-biao-by-tisfy-64z3/" target="_blank">LeetCode题解</a>|
5960
|0088.合并两个有序数组|简单|<a href="https://leetcode.cn/problems/merge-sorted-array/solutions/" target="_blank">题目地址</a>|<a href="https://blog.tisfy.eu.org/2023/08/13/LeetCode%200088.%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132256535" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/merge-sorted-array/solutions/2385633/letmefly-88he-bing-liang-ge-you-xu-shu-z-u9cc/" target="_blank">LeetCode题解</a>|
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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>给定一个已排序的链表的头&nbsp;<code>head</code> ,&nbsp;<em>删除原始链表中所有重复数字的节点,只留下不同的数字</em>&nbsp;。返回 <em>已排序的链表</em>&nbsp;。</p>
12+
13+
<p>&nbsp;</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>&nbsp;</p>
30+
31+
<p><strong>提示:</strong></p>
32+
33+
<ul>
34+
<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>
35+
<li><code>-100 &lt;= Node.val &lt;= 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

Comments
 (0)