题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if (head == null || head.next == null) return head;
ListNode res = new ListNode(-1);
res.next = head;
ListNode cur = res;
while (cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
ListNode temp = cur.next.next;
while (temp != null && temp.val == cur.next.val) {
temp = temp.next;
}
cur.next = temp;
} else {
cur = cur.next;
}
}
return res.next;
}
}
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类
* @return ListNode类
*/
public ListNode deleteDuplicates (ListNode head) {
// write code here
if (head == null || head.next == null) return head;
ListNode res = new ListNode(-1);
res.next = head;
ListNode cur = res;
while (cur.next != null && cur.next.next != null) {
if (cur.next.val == cur.next.next.val) {
ListNode temp = cur.next.next;
while (temp != null && temp.val == cur.next.val) {
temp = temp.next;
}
cur.next = temp;
} else {
cur = cur.next;
}
}
return res.next;
}
}