https://leetcode.cn/problems/intersection-of-two-linked-lists/description/?envType=problem-list-v2&envId=2cktkvj
双指针法
https://leetcode.cn/problems/intersection-of-two-linked-lists/solutions/12624/intersection-of-two-linked-lists-shuang-zhi-zhen-l/?envType=problem-list-v2&envId=2cktkvj
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
if headA == nil || headB == nil {
return nil
}
var cur1 *ListNode = headA
var cur2 *ListNode = headB
for cur1 != cur2 {
if cur1 != nil {
cur1 = cur1.Next
}else {
cur1 = headB
}
if cur2 != nil {
cur2 = cur2.Next
}else {
cur2 = headA
}
}
return cur1
}
map
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
visit := map[*ListNode]bool{}
for tmp:=headA;tmp != nil;tmp = tmp.Next {
visit[tmp] = true
}
for tmp:=headB;tmp!=nil;tmp = tmp.Next {
if visit[tmp] {
return tmp
}
}
return nil
}