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

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

Commit bce5050

Browse files
committed
Intersection of two linked lists
1 parent 8dd4dcc commit bce5050

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
*/
14+
var getIntersectionNode = function(headA, headB) {
15+
var array_a = [];
16+
var array_b = [];
17+
var ret = null;
18+
19+
while(headA) {
20+
array_a.push(headA);
21+
headA = headA.next;
22+
}
23+
24+
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];
39+
} else {
40+
return ret;
41+
}
42+
}
43+
};

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL