FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Intersection of two linked lists · didi0613/leetcode-javascript@a6f1b89 · GitHub

Commit a6f1b89

Browse files
committed
Intersection of two linked lists
1 parent 67d9ab9 commit a6f1b89

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

‎intersection-of-two-linked-lists.js‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,21 @@
1212
* @return {ListNode}
1313
*/
1414
var getIntersectionNode = function(headA, headB) {
15-
var array_a = [];
16-
var array_b = [];
15+
var hashmap = [];
1716
var ret = null;
18-
1917
while(headA) {
20-
array_a.push(headA);
18+
hashmap.push(headA.val);
2119
headA = headA.next;
2220
}
2321

2422
while(headB) {
25-
array_b.push(headB);
26-
headB = headB.next;
27-
}
28-
29-
var a_len = array_a.length;
30-
var b_len = array_b.length;
31-
32-
if(a_len < b_len) {
33-
return getIntersectionNode(headB,headA);
34-
}
35-
36-
for(var i=b_len-1,j=a_len-1;i>=0,j>=0;i--,j--) {
37-
if(array_b[i] !== array_a[j]) {
38-
ret = array_b[i].next;
23+
if(hashmap.includes(headB.val)) {
24+
ret = headB;
25+
break;
26+
} else {
27+
headB = headB.next;
3928
}
4029
}
30+
31+
return ret;
4132
};

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL