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

Commitec3a048

Browse files
authored
Merge pull requestneetcode-gh#279 from siddheshranade/siddheshranade-patch-1
Create 234-Palindrome-Linked-List.js
2 parents16eaa3c +79afc4b commitec3a048

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
vargetIntersectionNode=function(headA,headB){
15+
leta=headA;
16+
letb=headB;
17+
while(a!==b){
18+
a=a===null ?headB :a.next;
19+
b=b===null ?headA :b.next;
20+
}
21+
22+
returna;
23+
};
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, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
*@param {ListNode} head
10+
*@return {boolean}
11+
*/
12+
varisPalindrome=function(head){
13+
// find mid point
14+
letslow=head;
15+
letfast=head;
16+
while(fast&&fast.next){
17+
slow=slow.next;
18+
fast=fast.next.next;
19+
}
20+
21+
// reverse 2nd half
22+
letcurr=slow;
23+
letprev=null;
24+
while(curr){
25+
letnext=curr.next;
26+
curr.next=prev;
27+
prev=curr;
28+
curr=next;
29+
}
30+
lethead2=prev;
31+
32+
// compare both halfs
33+
while(head&&head2){
34+
if(head.val!==head2.val){
35+
returnfalse;
36+
}
37+
38+
head=head.next;
39+
head2=head2.next;
40+
}
41+
42+
returntrue;
43+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
*@param {number[]} nums
3+
*@param {number} target
4+
*@return {number}
5+
*/
6+
varsearchInsert=function(nums,target){
7+
letleft=0;
8+
letright=nums.length-1;
9+
while(left<=right){
10+
letmidIdx=Math.floor((left+right)/2);
11+
if(target===nums[midIdx]){
12+
returnmidIdx;
13+
}
14+
15+
if(target>nums[midIdx]){
16+
left=midIdx+1;
17+
}else{
18+
right=midIdx-1;
19+
}
20+
}
21+
22+
returnleft;
23+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp