Skip to content

Commit ec3a048

Browse files
authored
Merge pull request neetcode-gh#279 from siddheshranade/siddheshranade-patch-1
Create 234-Palindrome-Linked-List.js
2 parents 16eaa3c + 79afc4b commit ec3a048

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+
var getIntersectionNode = function(headA, headB) {
15+
let a = headA;
16+
let b = headB;
17+
while (a !== b) {
18+
a = a === null ? headB : a.next;
19+
b = b === null ? headA : b.next;
20+
}
21+
22+
return a;
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+
var isPalindrome = function(head) {
13+
// find mid point
14+
let slow = head;
15+
let fast = head;
16+
while (fast && fast.next) {
17+
slow = slow.next;
18+
fast = fast.next.next;
19+
}
20+
21+
// reverse 2nd half
22+
let curr = slow;
23+
let prev = null;
24+
while (curr) {
25+
let next = curr.next;
26+
curr.next = prev;
27+
prev = curr;
28+
curr = next;
29+
}
30+
let head2 = prev;
31+
32+
// compare both halfs
33+
while (head && head2) {
34+
if (head.val !== head2.val) {
35+
return false;
36+
}
37+
38+
head = head.next;
39+
head2 = head2.next;
40+
}
41+
42+
return true;
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+
var searchInsert = function(nums, target) {
7+
let left = 0;
8+
let right = nums.length - 1;
9+
while (left <= right) {
10+
let midIdx = Math.floor((left + right) / 2);
11+
if (target === nums[midIdx]) {
12+
return midIdx;
13+
}
14+
15+
if (target > nums[midIdx]) {
16+
left = midIdx + 1;
17+
} else {
18+
right = midIdx - 1;
19+
}
20+
}
21+
22+
return left;
23+
};

0 commit comments

Comments
 (0)