题解 | #删除有序链表中重复的元素-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) {
int[] cnt = new int[2001];
ListNode res = new ListNode(0);
res.next = head;
ListNode p = head;
ListNode q = res;
while (head != null) {
cnt[head.val+1000]++;
head = head.next;
}
while (p != null) {
if (cnt[p.val+1000] > 1) {
ListNode next = p.next;
p.next = null;
q.next = next;
p = next;
} else {
q = p ;
p = p.next;
}
}
return res.next;
}
}


查看11道真题和解析