代码例如以下:
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode helper = new ListNode(0);
helper.next = head;
ListNode pre = helper;
ListNode cur = head;
while(cur!=null)
{
while(cur.next!=null && pre.next.val==cur.next.val)
{
cur = cur.next;
}
if(pre.next==cur)
{
pre = pre.next;
}
else
{
pre.next = cur.next;
}
cur = cur.next;
}
return helper.next;
}能够看到,上述代码中我们创建了一个辅助的头指针。是为了改动链表头的方便。前面介绍过,通常会改到链表头的题目就会须要一个辅助指针,是比較常见的技巧。Remove Duplicates from Sorted List II -- LeetCode
原文:http://www.cnblogs.com/zhchoutai/p/7195903.html