algorithm-base/animation-simulation/链表篇/leetcode82删除排序链表中的重复元素II.md

168 lines
6.4 KiB
Java
Raw Normal View History

2021-03-20 08:57:12 +00:00
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [82. II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/)
2021-03-19 07:07:33 +00:00
****
2021-03-19 07:07:33 +00:00
1:
```java
: 1->2->3->3->4->4->5
: 1->2->5
```
2:
```java
: 1->1->1->2->3
: 2->3
```
> 112323
2021-03-19 07:07:33 +00:00
![2](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/删除重复节点2.3btmii5cgxa0.gif)
2021-04-28 10:28:00 +00:00
****
Java Code:
2021-03-19 07:07:33 +00:00
```java
class Solution {
public ListNode deleteDuplicates(ListNode head) {
//侦察兵指针
2021-03-19 07:07:33 +00:00
ListNode pre = head;
2021-07-15 16:06:52 +00:00
//创建哑节点接上head
ListNode dummy = new ListNode(-1);
2021-07-15 16:06:52 +00:00
dummy.next = head;
//跟随的指针
ListNode low = dummy;
2021-03-19 07:07:33 +00:00
while(pre != null && pre.next != null) {
if (pre.val == pre.next.val) {
//移动侦察兵指针直到找到与上一个不相同的元素
2021-03-19 07:07:33 +00:00
while (pre != null && pre.next != null && pre.val == pre.next.val) {
pre = pre.next;
}
//while循环后pre停留在最后一个重复的节点上
2021-03-19 07:07:33 +00:00
pre = pre.next;
//连上新节点
2021-03-19 07:07:33 +00:00
low.next = pre;
}
else{
pre = pre.next;
low = low.next;
}
}
return dummy.next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
2021-03-19 07:07:33 +00:00
}
}
```
2021-04-28 10:28:00 +00:00
C++ Code:
```cpp
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
//侦察兵指针
2021-04-28 10:28:00 +00:00
ListNode * pre = head;
2021-07-15 16:06:52 +00:00
//创建哑节点接上head
ListNode * dummy = new ListNode(-1);
dummy->next = head;
//跟随的指针
ListNode * low = dummy;
2021-04-28 10:28:00 +00:00
while(pre != nullptr && pre->next != nullptr) {
if (pre->val == pre->next->val) {
//移动侦察兵指针直到找到与上一个不相同的元素
2021-04-28 10:28:00 +00:00
while (pre != nullptr && pre->next != nullptr && pre->val == pre->next->val) {
pre = pre->next;
}
//while循环后pre停留在最后一个重复的节点上
2021-04-28 10:28:00 +00:00
pre = pre->next;
//连上新节点
2021-04-28 10:28:00 +00:00
low->next = pre;
}
else{
pre = pre->next;
low = low->next;
}
}
return dummy->next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
2021-04-28 10:28:00 +00:00
}
};
```
JS Code:
```javascript
var deleteDuplicates = function(head) {
//侦察兵指针
let pre = head;
//创建虚拟头节点接上head
let dummy = new ListNode(-1);
dummy.next = pre;
//跟随的指针
let low = dummy;
while(pre != null && pre.next != null) {
if (pre.val == pre.next.val) {
//移动侦察兵指针直到找到与上一个不相同的元素
while (pre != null && pre.next != null && pre.val === pre.next.val) {
pre = pre.next;
}
//while循环后pre停留在最后一个重复的节点上
pre = pre.next;
//连上新节点
low.next = pre;
}
else{
pre = pre.next;
low = low.next;
}
}
return dummy.next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
};
```
Python Code:
2021-07-15 16:06:52 +00:00
```python
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
#
pre = head
# head
dummy = ListNode(-1, head)
#
low = dummy
while pre is not None and pre.next is not None:
if pre.val == pre.next.val:
#
while pre is not None and pre.next is not None and pre.val == pre.next.val:
pre = pre.next
# whilepre
pre = pre.next
#
low.next = pre
else:
pre = pre.next
low = low.next
2021-07-15 16:06:52 +00:00
return dummy.next # headhead
```