# Intersection of Two Linked Lists

ID: 160; easy

## Solution 1 (Go)

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/

ignore := lengthA - lengthB
if ignore > 0 {
for ignore > 0 {
ignore--
}
} else if ignore < 0 {
for ignore < 0 {
ignore++
}
}
}
}
}
return nil
}

func getLength(root *ListNode) int {
length := 0
for root != nil {
length++
root = root.Next
}
return length
}``````

## Solution 2 (Go)

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/

return nil
}
// a little bit of trickery
for a != b {
if a == nil {
} else {
a = a.Next
}
if b == nil {
} else {
b = b.Next
}
}
return a
}``````

## Solution 3 (Java)

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) {
*         val = x;
*         next = null;
*     }
* }
*/
public class Solution {

while (currA != currB) {
if (currA == null) {
} else {
currA = currA.next;
}
if (currB == null) {
} else {
currB = currB.next;
}
}
return currA;
}
}``````

Last updated