Skip to content

Commit 9bb8b90

Browse files
committed
add files
1 parent f9a9c42 commit 9bb8b90

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

IntersectionOfTwoLinkedLists.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Leetcode #160
2+
// Name: Intersection of Two Linked Lists
3+
// Language: Javascript
4+
// Problem: https://leetcode.com/problems/intersection-of-two-linked-lists/
5+
6+
7+
/**
8+
* Definition for singly-linked list.
9+
* function ListNode(val) {
10+
* this.val = val;
11+
* this.next = null;
12+
* }
13+
*/
14+
15+
/**
16+
* @param {ListNode} headA
17+
* @param {ListNode} headB
18+
* @return {ListNode}
19+
*/
20+
var getIntersectionNode = function(headA, headB) {
21+
var lenA = getLen(headA);
22+
var lenB = getLen(headB);
23+
24+
if(lenA === 0 || lenB === 0){
25+
return null;
26+
}
27+
28+
while(lenA > lenB){
29+
headA = headA.next;
30+
lenA--;
31+
}
32+
33+
while(lenB > lenA){
34+
headB = headB.next;
35+
lenB--;
36+
}
37+
38+
while(lenA && lenB){
39+
if(headB === headA){
40+
return headA;
41+
}
42+
43+
headA = headA.next;
44+
headB = headB.next;
45+
}
46+
47+
return null;
48+
};
49+
50+
var getLen = function(head){
51+
var len = 0;
52+
53+
while(head){
54+
head = head.next;
55+
len++;
56+
}
57+
58+
return len;
59+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {TreeNode}
13+
*/
14+
var lowestCommonAncestor = function(root, p, q) {
15+
if(root === null) {
16+
return root;
17+
}
18+
19+
if(root === p || root === q) {
20+
return root;
21+
}
22+
23+
if((root.val >= p.val && root.val <= q.val) || (root.val <= p.val && root.val >= q.val)){
24+
return root;
25+
}
26+
27+
if(root.val > p.val && root.val > q.val){
28+
return lowestCommonAncestor(root.left, p, q);
29+
} else {
30+
return lowestCommonAncestor(root.right, p, q);
31+
}
32+
};

PathSum.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Leetcode #112
2+
// Name: Path Sum
3+
// Language: Javascript
4+
// Problem: https://leetcode.com/problems/path-sum/
5+
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* function TreeNode(val) {
10+
* this.val = val;
11+
* this.left = this.right = null;
12+
* }
13+
*/
14+
/**
15+
* @param {TreeNode} root
16+
* @param {number} sum
17+
* @return {boolean}
18+
*/
19+
var hasPathSum = function(root, sum) {
20+
if(root === null){
21+
return false;
22+
}
23+
24+
if(root.val === sum && root.left === null && root.right === null){
25+
return true;
26+
}
27+
28+
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
29+
};

0 commit comments

Comments
 (0)