Skip to content

Commit 4a475c3

Browse files
committed
solved: 160. Intersection of Two Linked Lists
Signed-off-by: rajput-hemant <[email protected]>
1 parent cb94794 commit 4a475c3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
// https://leetcode.com/problems/intersection-of-two-linked-lists/solutions/49785/java-solution-without-knowing-the-difference-in-len/
10+
func getIntersectionNode(headA, headB *ListNode) *ListNode {
11+
if headA == nil || headB == nil {
12+
return nil
13+
}
14+
15+
a, b := headA, headB
16+
17+
for a != b {
18+
if a == nil {
19+
a = headB
20+
} else {
21+
a = a.Next
22+
}
23+
24+
if b == nil {
25+
b = headA
26+
} else {
27+
b = b.Next
28+
}
29+
}
30+
31+
return a
32+
}

0 commit comments

Comments
 (0)