Files
LeetCode-Go/leetcode/0160.Intersection-of-Two-Linked-Lists/160. Intersection of Two Linked Lists.go
2020-08-07 17:06:53 +08:00

46 lines
778 B
Go

package leetcode
import "fmt"
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// ListNode define
type ListNode = structures.ListNode
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
//boundary check
if headA == nil || headB == nil {
return nil
}
a := headA
b := headB
//if a & b have different len, then we will stop the loop after second iteration
for a != b {
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
if a == nil {
a = headB
} else {
a = a.Next
}
if b == nil {
b = headA
} else {
b = b.Next
}
fmt.Printf("a = %v b = %v\n", a, b)
}
return a
}