-
Notifications
You must be signed in to change notification settings - Fork 0
/
52-mid.go
105 lines (91 loc) · 1.68 KB
/
52-mid.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import "fmt"
/**
【简单|简单】
输入两个链表,找出它们的第一个公共节点。
如下面的两个链表:
在节点 c1 开始相交。
intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
【解题】先map,在循环第二个
【卡点】难点,map的键值要存*ListNode 即指针地址,不能是val
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
type ListNode struct {
Val int
Next *ListNode
}
func getIntersectionNode(headA, headB *ListNode) *ListNode {
find := make(map[*ListNode]bool)
for headA !=nil{
find[headA] = true
headA = headA.Next
}
for headB!=nil{
if _,ok:=find[headB];ok{
return headB
}
headB = headB.Next
}
return nil
}
func main() {
fmt.Println(test1())
fmt.Println(test2())
}
func test1() *ListNode {
//公共
lc := ListNode{
Val: 8,
Next: &ListNode{
Val: 4,
Next: &ListNode{
Val: 5,
Next: nil,
},
},
}
l1 := ListNode{
Val: 4,
Next: &ListNode{
Val: 1,
Next: &lc,
},
}
l2 := ListNode{
Val: 5,
Next: &ListNode{
Val: 0,
Next: &ListNode{
Val: 1,
Next: &lc,
},
},
}
//headA:=ListNode{
// Val: 0,
// Next: &l1,
//}
//headB:=ListNode{
// Val: 0,
// Next: &l2,
//}
return getIntersectionNode(&l1,&l2)
}
func test2() *ListNode {
//公共
lc := ListNode{
Val: 1,
Next: nil,
}
//headA:=ListNode{
// Val: 0,
// Next: &lc,
//}
//headB:=ListNode{
// Val: 0,
// Next: &lc,
//}
return getIntersectionNode(&lc,&lc)
}