mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
46 lines
778 B
Go
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
|
|
}
|