题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
因为已经升序排序了,就想到用快慢指针。
public ListNode deleteDuplicates (ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode pre = head;
ListNode pos = head.next;
while(pos != null)
{
if(pre.val == pos.val)
{
pre.next = pos.next;
pos = pre.next;
}
else
{
pre = pre.next;
pos = pos.next;
}
}
return head;
}

查看7道真题和解析