题解 | #删除有序链表中重复的元素-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) { if(head == null) return null; ListNode dummy = new ListNode(1001); dummy.next = head; ListNode cur = head; ListNode pre = dummy; while(cur!=null&&cur.next!=null){ if(cur.val==cur.next.val){ int tmp = cur.val; while(cur!=null && cur.val==tmp){ cur = cur.next; } pre.next = cur; }else{ pre = pre.next; cur = cur.next; } } return dummy.next; // write code here } }