Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
For example, the following two linked lists begin to intersect at node c1:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
listA is in the m.listB is in the n.1 <= m, n <= 3 * 10^41 <= Node.val <= 10^5
Follow up: Could you write a solution that runs in O(m + n) time and use only O(1) memory?
No developer has claimed the Java throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
/**
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
var nodeCountA = 0;
var nodeCountB = 0;
var node = headA;
while (node != null) {
nodeCountA += 1;
node = node.next;
}
node = headB;
while (node != null) {
nodeCountB += 1;
node = node.next;
}
var bigger = headA;
var smaller = headB;
if (nodeCountB > nodeCountA) {
bigger = headB;
smaller = headA;
}
for (var i = 0; i < Math.abs(nodeCountB - nodeCountA); i++) {
bigger = bigger.next;
}
while (bigger != null && smaller != null) {
if (bigger == smaller) {
return bigger;
}
bigger = bigger.next;
smaller = smaller.next;
}
return null;
}
}
No developer has claimed the Python throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
node_count_a = 0
node_count_b = 0
node = headA
while node:
node_count_a += 1
node = node.next
node = headB
while node:
node_count_b += 1
node = node.next
bigger = headA
smaller = headB
if node_count_b > node_count_a:
bigger = headB
smaller = headA
for _ in range(abs(node_count_b - node_count_a)):
bigger = bigger.next
while bigger and smaller:
if bigger == smaller:
return bigger
bigger = bigger.next
smaller = smaller.next
return None
No developer has claimed the Javascript throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
var getIntersectionNode = function (headA, headB) {
let nodeCountA = 0
let nodeCountB = 0
let node = headA;
while (node != null) {
nodeCountA += 1
node = node.next
}
node = headB
while (node != null) {
nodeCountB += 1
node = node.next
}
let bigger = headA
let smaller = headB
if (nodeCountB > nodeCountA) {
bigger = headB
smaller = headA
}
for (var i = 0; i < Math.abs(nodeCountB - nodeCountA); i++) {
bigger = bigger.next
}
while (bigger != null && smaller != null) {
if (bigger == smaller) {
return bigger
}
bigger = bigger.next
smaller = smaller.next
}
return null
};
No developer has claimed the C# throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
/**
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
public ListNode GetIntersectionNode(ListNode headA, ListNode headB)
{
int nodeCountA = 0;
int nodeCountB = 0;
var node = headA;
while (node != null)
{
nodeCountA += 1;
node = node.next;
}
node = headB;
while (node != null)
{
nodeCountB += 1;
node = node.next;
}
var bigger = headA;
var smaller = headB;
if (nodeCountB > nodeCountA)
{
bigger = headB;
smaller = headA;
}
for (int i = 0; i < Math.Abs(nodeCountB - nodeCountA); i++)
{
bigger = bigger.next;
}
while (bigger != null && smaller != null)
{
if (bigger == smaller)
{
return bigger;
}
bigger = bigger.next;
smaller = smaller.next;
}
return null;
}
}
No developer has claimed the Ruby throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val)
# @val = val
# @next = nil
# end
# end
# @param {ListNode} head_a
# @param {ListNode} head_b
# @return {ListNode}
def getIntersectionNode(head_a, head_b)
node_count_a = 0
node_count_b = 0
node = head_a
while node
node_count_a += 1
node = node.next
end
node = head_b
while node
node_count_b += 1
node = node.next
end
bigger = head_a
smaller = head_b
if node_count_b > node_count_a
bigger = head_b
smaller = head_a
end
(node_count_b - node_count_a).abs.times do
bigger = bigger.next
end
while bigger && smaller
return bigger if bigger == smaller
bigger = bigger.next
smaller = smaller.next
end
nil
end
No developer has claimed the C++ throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *head_a, ListNode *head_b) {
int node_count_a = 0;
int node_count_b = 0;
ListNode *node = head_a;
while (node) {
node_count_a += 1;
node = node->next;
}
node = head_b;
while (node) {
node_count_b += 1;
node = node->next;
}
ListNode *bigger = head_a;
ListNode *smaller = head_b;
if (node_count_b > node_count_a) {
bigger = head_b;
smaller = head_a;
}
for (int i = 0; i < abs(node_count_b - node_count_a); i++) {
bigger = bigger->next;
}
while (bigger && smaller) {
if (bigger == smaller) {
return bigger;
}
bigger = bigger->next;
smaller = smaller->next;
}
return nullptr;
}
};
No developer has claimed the Go throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
This is a typical "encounter" problem, and it is best to transform it into a real-life scenario to enhance understanding.
Suppose you are A, and you are pursuing B. The destination is school. On the way to school, the distance at the back is what you both have to go through, and the distance at the front is for each of you to go your own way. The node spacing is assumed to be one kilometer.
Now, one morning, you both finished breakfast at the same time and are going to ride your bike to school. And you have a goal: to meet B and chat with him/her for a few words. What will you do? (Take Example 1 as an example)
Click to view the answer
You must first calculate how many kilometers her home is farther from the school than your home, and then wait for her to walk these kilometers before setting off. As long as you have the same speed, you will definitely meet, and the node where you meet is the answer.
node_count_a, and the number of nodes in linked list B is node_count_b.node_count_b > node_count_a, then perform node = node.next for node_count_b - node_count_a times on linked list B.node = node.next on the two linked lists until the same node is found or one of the linked lists has reached the end.
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
nodeCountA := 0
nodeCountB := 0
node := headA
for node != nil {
nodeCountA++
node = node.Next
}
node = headB
for node != nil {
nodeCountB++
node = node.Next
}
bigger := headA
smaller := headB
if nodeCountB > nodeCountA {
bigger = headB
smaller = headA
}
difference := nodeCountB - nodeCountA
if difference < 0 {
difference = -difference
}
for i := 0; i < difference; i++ {
bigger = bigger.Next
}
for bigger != nil && smaller != nil {
if bigger == smaller {
return bigger
}
bigger = bigger.Next
smaller = smaller.Next
}
return nil
}
No developer has claimed the Rust throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #160 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.