Tianhe Gao

LC 160. Intersection of Two Linked Lists

 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  const visited = new Set()
16  let temp = headA
17  while (temp !== null) {
18    visited.add(temp)
19    temp = temp.next
20  }
21  temp = headB
22  while (temp !== null) {
23    if (visited.has(temp)) {
24      return temp
25    }
26    temp = temp.next
27  }
28  return null
29};

No notes link to this note

Welcome to tell me your thoughts via "email"
UP