LeetCode: Intersection Of Two Linked Lists Solution

1/**
2 * Definition for singly-linked list.
3 * function ListNode(val) {
4 * this.val = val;
5 * this.next = null;
6 * }
7 */
8
9/**
10 * @param {ListNode} headA
11 * @param {ListNode} headB
12 * @return {ListNode}
13 */
14var getIntersectionNode = function (headA, headB) {
15 let res = null
16 let set = new WeakSet()
17 while (headA) {
18 set.add(headA)
19 headA = headA.next
20 }
21 while (headB) {
22 if (set.has(headB)) {
23 res = headB
24 break
25 }
26 headB = headB.next
27 }
28 return res
29}

Comments

Loading comments...

Tags

leetcode

linked list

Apply and earn a $2,500 bonus once you're hired on your first job!

Clients from the Fortune 500 to Silicon Valley startups

Choose your own rate, get paid on time

From hourly, part-time, to full-time positions

Flexible remote working environment

A lot of open JavaScript jobs!!

Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.

Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.

Next Post

LeetCode: Short Encoding Of Words

Mar 7, 2021

HoningJS

Search Posts