/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode pre = dummy, cur = dummy.next;
while(cur != null){
while(cur.next != null && cur.val == cur.next.val){
cur = cur.next;
}
if(pre.next == cur){
pre = pre.next;
cur = cur.next;
}else{
pre.next = cur.next;
cur = cur.next;
}
}
return dummy.next;
}
}
[LeetCode] 82. Remove Duplicates from Sorted List II
最新推荐文章于 2023-08-30 08:07:21 发布