题解 | #删除有序链表中重复的元素-II#
删除有序链表中重复的元素-II
http://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024
package main
import . "nc_tools"
/*
* type ListNode struct{
* Val int
* Next *ListNode
* }
*/
/**
*
* @param head ListNode类
* @return ListNode类
*/
func deleteDuplicates( head *ListNode ) *ListNode {
// write code here
if head == nil || head.Next == nil {
return head
}
ht1 := make(map[int]bool)
ht2 := make(map[int]bool)
tmp := head
for tmp != nil {
if ht1[tmp.Val] {
ht2[tmp.Val] = true
}
ht1[tmp.Val] = true
tmp = tmp.Next
}
tmp = head
for tmp != nil && ht2[tmp.Val] {
tmp = tmp.Next
}
head = tmp
tmp = head
for tmp != nil {
for tmp.Next != nil && ht2[tmp.Next.Val] {
tmp.Next = tmp.Next.Next
}
tmp = tmp.Next
}
return head
}