题解 | #删除有序链表中重复的元素-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 dummy = new ListNode(-1); dummy.next = head; ListNode slow = dummy; ListNode fast = head; while (fast != null && fast.next != null) { if (fast.val == fast.next.val) { int tmp = fast.val; while (fast != null && tmp == fast.val) { fast = fast.next; } } else { slow.next = fast; slow = fast; if (fast != null) { fast = fast.next; } } } slow.next = fast; return dummy.next; } public ListNode deleteDuplicates2 (ListNode head) { // write code here if (head == null || head.next == null) { return head; } Map<Integer, Integer> map = new HashMap<>(); ListNode dummy = new ListNode(-1); ListNode tail = dummy; ListNode cur = head; while (cur != null) { map.put(cur.val, map.getOrDefault(cur.val, 0) + 1); cur = cur.next; } cur = head; while (cur != null) { if (map.get(cur.val) == 1) { tail.next = cur; tail = cur; } cur = cur.next; } tail.next = null; return dummy.next; } }