-
Notifications
You must be signed in to change notification settings - Fork 480
/
Copy pathdetect_intersections.go
40 lines (34 loc) · 1 KB
/
detect_intersections.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
package detectintersections
import "github.com/shomali11/go-interview/datastructures/linkedlists/singlylinkedlists"
// DoIntersect checks if the two lists intersect
func DoIntersect[T comparable](head1 *singlylinkedlists.SLLNode[T], head2 *singlylinkedlists.SLLNode[T]) bool {
head1ListCount := getCountNodes(head1)
head2ListCount := getCountNodes(head2)
current1 := getStartingNode(head1, head1ListCount-head2ListCount)
current2 := getStartingNode(head2, head2ListCount-head1ListCount)
for current1 != nil {
if current1 == current2 {
return true
}
current1 = current1.Next
current2 = current2.Next
}
return false
}
func getStartingNode[T comparable](head *singlylinkedlists.SLLNode[T], moves int) *singlylinkedlists.SLLNode[T] {
current := head
for moves > 0 {
current = current.Next
moves--
}
return current
}
func getCountNodes[T comparable](head *singlylinkedlists.SLLNode[T]) int {
count := 0
current := head
for current != nil {
current = current.Next
count++
}
return count
}