350. Find Common Elements in Two Sorted Linked Lists

EasyLinked ListLinked ListTwo PointerIntersectionSorted

Given two sorted singly linked lists, return a sorted array of all elements that appear in both lists. Each element appears only once in the result.

Input: Heads of two sorted singly linked lists.

Output: Sorted array of common elements.

Examples

Example 1
Input: [1,2,3,4,5], [3,4,5,6,7]
Output: [3,4,5]
Explanation: Common: 3,4,5.
Example 2
Input: [1,2,3], [4,5,6]
Output: []
Explanation: No common elements.
Example 3
Input: [1,2,3], [1,2,3]
Output: [1,2,3]
Explanation: All common.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →