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

71 lines
3.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

> **[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/)
1:
```java
: 1->2->3->3->4->4->5
: 1->2->5
```
2:
```java
: 1->1->1->2->3
: 2->3
```
> 112323
![2](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/删除重复节点2.3btmii5cgxa0.gif)
```java
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null||head.next==null){
return head;
}
ListNode pre = head;
ListNode low = new ListNode(0);
low.next = pre;
ListNode ret = new ListNode(-1);
ret = low;
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;
}
pre = pre.next;
low.next = pre;
}
else{
pre = pre.next;
low = low.next;
}
}
return ret.next;
}
}
```