Files
leetcode/82.remove-duplicates-from-sorted-list-ii.java
2025-10-27 16:22:33 +08:00

43 lines
1.0 KiB
Java

/*
* @lc app=leetcode id=82 lang=java
*
* [82] Remove Duplicates from Sorted List II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
dummy.next = null;
ListNode cur = head;
int v;
while(cur != null) {
if(cur.next == null || cur.val != cur.next.val) {
tail.next = cur;
cur = cur.next;
tail = tail.next;
tail.next = null;
} else {
v = cur.val;
while(cur != null && cur.val == v) {
cur = cur.next;
}
}
}
return dummy.next;
}
}
// @lc code=end