Leecode no.82 删除排序链表中的重复元素 II

package leecode;

/**
* 82. 删除排序链表中的重复元素 II
*
* 存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。
*
* 返回同样按升序排列的结果链表。
*
* @author Tang
* @date 2021/12/21
*/
public class DeleteDuplicates2 {

/**
* 存一个value 保存的是上一个该删除的元素值
*
*
* @param head
* @return
*/
public ListNode deleteDuplicates(ListNode head) {
if(head == null) {
return null;
}

ListNode first = new ListNode();
first.next = head;
int value = Integer.MIN_VALUE;

ListNode temp = first;
while (head.next != null) {
if(head.val == head.next.val || head.val == value) {
//记录下这个重复的值
value = head.val;
//删掉head
temp.next = head.next;
} else {
temp = temp.next;
}

head = head.next;
}

if(head.val == value) {
temp.next = null;
}
return first.next;
}


public static void main(String[] args) {

}





}
原文地址:https://www.cnblogs.com/ttaall/p/15717255.html