Problem: 82. 删除排序链表中的重复元素 II
Code
/**
* 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, head);
ListNode cur = dummy;
while(cur.next != null && cur.next.next != null){
int val = cur.next.val;//这里是下一个节点的值
if(cur.next.next.val == val)//重复的情况
{
while(cur.next != null && cur.next.val == val){
cur.next = cur.next.next; //删除与重复值相等的所有节点
}
}else{ // 不重复则跳过继续处理下一个
cur = cur.next;
}
}
return dummy.next;
}
}