-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.go
43 lines (37 loc) · 653 Bytes
/
solution.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 main
import (
"fmt"
)
// ListNode is a singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}
func getIntersectionNode(headA, headB *ListNode) *ListNode {
if headA == nil || headB == nil {
return nil
}
a := headA
b := headB
for a != b {
if a == nil {
a = headB
} else {
a = a.Next
}
if b == nil {
b = headA
} else {
b = b.Next
}
}
return a
}
func main() {
headA := &ListNode{2, &ListNode{4, nil}}
headB := &ListNode{9, &ListNode{1, headA}}
headIntersection := getIntersectionNode(headA, headB)
for head := headIntersection; head != nil; head = head.Next {
fmt.Println(head.Val)
}
}