Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9bb8b90

Browse files
committed
add files
1 parentf9a9c42 commit9bb8b90

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+
vargetIntersectionNode=function(headA,headB){
21+
varlenA=getLen(headA);
22+
varlenB=getLen(headB);
23+
24+
if(lenA===0||lenB===0){
25+
returnnull;
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+
returnheadA;
41+
}
42+
43+
headA=headA.next;
44+
headB=headB.next;
45+
}
46+
47+
returnnull;
48+
};
49+
50+
vargetLen=function(head){
51+
varlen=0;
52+
53+
while(head){
54+
head=head.next;
55+
len++;
56+
}
57+
58+
returnlen;
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+
varlowestCommonAncestor=function(root,p,q){
15+
if(root===null){
16+
returnroot;
17+
}
18+
19+
if(root===p||root===q){
20+
returnroot;
21+
}
22+
23+
if((root.val>=p.val&&root.val<=q.val)||(root.val<=p.val&&root.val>=q.val)){
24+
returnroot;
25+
}
26+
27+
if(root.val>p.val&&root.val>q.val){
28+
returnlowestCommonAncestor(root.left,p,q);
29+
}else{
30+
returnlowestCommonAncestor(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+
varhasPathSum=function(root,sum){
20+
if(root===null){
21+
returnfalse;
22+
}
23+
24+
if(root.val===sum&&root.left===null&&root.right===null){
25+
returntrue;
26+
}
27+
28+
returnhasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
29+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp