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 */89/**10 * @param {ListNode} headA11 * @param {ListNode} headB12 * @return {ListNode}13 */14var getIntersectionNode = function (headA, headB) {15 let res = null16 let set = new WeakSet()17 while (headA) {18 set.add(headA)19 headA = headA.next20 }21 while (headB) {22 if (set.has(headB)) {23 res = headB24 break25 }26 headB = headB.next27 }28 return res29}
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.