题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
package main
import . "nc_tools"
/*
* type ListNode struct{
* Val int
* Next *ListNode
* }
*/
func deleteDuplication(head *ListNode ) *ListNode {
if head == nil || head.Next == nil {
return head
}
//The first element is the repeating element
sentry := new(ListNode)
sentry.Next = head
prev, cur := sentry, head
for cur != nil {
if cur.Next != nil && cur.Val == cur.Next.Val {
for cur.Next != nil && cur.Val == cur.Next.Val {
cur = cur.Next
}
prev.Next = cur.Next
cur = cur.Next
continue
}
cur = cur.Next
prev = prev.Next
}
return sentry.Next
}