-
Notifications
You must be signed in to change notification settings - Fork 0
/
082.go
43 lines (36 loc) · 807 Bytes
/
082.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package p082
/**
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
*/
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
type ListNode struct {
Val int
Next *ListNode
}
func deleteDuplicates(head *ListNode) *ListNode {
head = &ListNode{0, head}
quick, slow := head.Next, head
for quick != nil {
if quick.Next != nil && quick.Next.Val == quick.Val {
dupv := quick.Val
for quick != nil && quick.Val == dupv {
quick = quick.Next
}
} else {
slow.Next = quick
slow = slow.Next
quick = quick.Next
}
}
slow.Next = quick
return head.Next
}