坚持每天力扣每日一题(1)
题目
82. 删除排序链表中的重复元素 II
语言:Java
/**
* 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 res = new ListNode(0, head), flag = res;
while(flag.next != null && flag.next.next != null){
int a = flag.next.val;
if(flag.next.next.val == a){
while(flag.next != null && flag.next.val == a){
flag.next = flag.next.next;
}
}
else{
flag = flag.next;
}
}
return res.next;
}
}