142. Intersection of Two Linked Lists

EasyLinked List

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. The two lists may share a common suffix of nodes. The test harness builds list A, then builds list B's private prefix and links it to A's node at index skipA (or leaves B independent when skipA is -1). Return the value of the intersection node, or null if there is none. The linked lists must retain their original structure after the function returns.

Examples

Input: [4,1,8,4,5] 2 [5,6,1]

Output: 8

Explanation: A = 4->1->8->4->5, B = 5->6->1->8->4->5; they share the suffix starting at 8.

Constraints

  • The number of nodes of listA is in the m.
  • The number of nodes of listB is in the n.
  • 1 <= m, n <= 3 * 10^4
  • 1 <= Node.val <= 10^5
  • There are no cycles anywhere in the entire linked structure.
Loading...

Run checks all cases above. Submit evaluates all test cases.