package main
import . "nc_tools"

/*
//递归
func deleteDuplicates( head *ListNode ) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }

    if head.Val == head.Next.Val {
        for head.Next != nil && head.Val == head.Next.Val {
            head = head.Next
        }
        return deleteDuplicates(head.Next)
    } else {
        head.Next = deleteDuplicates(head.Next)
        return head
    }
}
*/

//迭代
func deleteDuplicates( head *ListNode ) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }

    dummy := &ListNode{Next : head}
    pre := dummy

    for pre.Next != nil && pre.Next.Next != nil {
        if pre.Next != nil && pre.Next.Val == pre.Next.Next.Val {
            x := pre.Next.Val

            for pre.Next != nil && pre.Next.Val == x {
                pre.Next = pre.Next.Next
            }
        } else {
            pre = pre.Next
        }
    }
    return dummy.Next
}