Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Intersection of Two Linked Lists

Medium Acceptance 75.00% Points 30.00

You are given the heads of two singly linked lists that may intersect. Your task is to return the value of the node where the two linked lists intersect. If the linked lists do not intersect, return -1.

Examples
Example 1

Input: list1 = [4,1,8,4,5], list2 = [5,6,1,8,4,5] Output: 8 Explanation: Both linked lists intersect at the node with value 8.

Input: list1 = [1,9,1,2,4], list2 = [3,2,4] Output: 2 Explanation: The two linked lists intersect at the node with value 2.

Input: list1 = [2,6,4], list2 = [1,5] Output: -1 Explanation: The two linked lists do not intersect.

Constraints
  • 0 = Number of nodes in each list = 105
  • -10? = Node.data = 10?
Companies
Samsung
Topics
Linked List
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions