-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3a8403
commit db8a825
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {boolean} | ||
*/ | ||
var isPalindrome = function(head) { | ||
/** | ||
* solution 01 | ||
*/ | ||
|
||
// let left = head | ||
// let counter = 0 | ||
// | ||
// const traverse = (right) => { | ||
// if (right === null) return true | ||
// let res = traverse(right.next) | ||
// | ||
// res = res && (left.val === right.val) | ||
// left = left.next | ||
// return res | ||
// } | ||
// | ||
// return traverse(head) | ||
|
||
/** | ||
* solution 02 | ||
*/ | ||
|
||
const reverse = (head) => { | ||
let cur = head | ||
let prev = null | ||
let next = null | ||
while (cur !== null) { | ||
next = cur.next | ||
cur.next = prev | ||
prev = cur | ||
cur = next | ||
} | ||
return prev | ||
} | ||
|
||
let slow = head | ||
let fast = head | ||
|
||
while (fast !== null && fast.next !== null) { | ||
slow = slow.next | ||
fast = fast.next.next | ||
} | ||
|
||
if (fast !== null) slow = slow.next | ||
|
||
let left = head | ||
let right = reverse(slow) | ||
|
||
while (right !== null) { | ||
if (left.val !== right.val) return false | ||
left = left.next | ||
right = right.next | ||
} | ||
return true | ||
}; | ||
|
||
// 2022/05/08 done. | ||
// Runtime: 203 ms, faster than 50.96% of JavaScript online submissions for Palindrome Linked List. | ||
// Memory Usage: 68.7 MB, less than 89.18% of JavaScript online submissions for Palindrome Linked List. |