Just a quick note that may trip people up , line 42 the mid name might be a bit of misnomer, the reverse will return the previous which is the very end of the linkedlist to the right . We are really comparing the very end to the very beginning of linkedlist in the first iteration.
and also, len is a little bit misleading, because len is only half the lenght of the arr, which is used to compare the reversed and the original only to the half of the linked list
Hey really nice video :) Thanks for thath. Just a small nit. There is no need for the len variable. Since we reverse the second half of the linked list we can just call while(mid) on the second while loop cuz mid will point to null eventually. /** * 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} */ const reverseList = head =>{ let cur = head; let prev = null; let next = null; while(cur){ next = cur.next; cur.next = prev; prev = cur; cur = next; } return prev; } var isPalindrome = function(head) { let slow = head; let fast = head; // fast.next cuz we use fast.next.next and that would return an error if fast.next is null while(fast && fast.next){ fast = fast.next.next; slow = slow.next; } let mid = reverseList(slow); let pointer = head; while(mid){ if(mid.val !== pointer.val){ return false } mid = mid.next; pointer = pointer.next; } return true; };
It's kind of insane that such high quality information is completely free.
Thanks a lot man, you are changing lives.
Thanks @Andy Gala ,but pls why is this consider easy?
your white board illustrations are a very nice addition
Just a quick note that may trip people up , line 42 the mid name might be a bit of misnomer, the reverse will return the previous which is the very end of the linkedlist to the right . We are really comparing the very end to the very beginning of linkedlist in the first iteration.
and also, len is a little bit misleading, because len is only half the lenght of the arr, which is used to compare the reversed and the original only to the half of the linked list
Hey really nice video :) Thanks for thath. Just a small nit. There is no need for the len variable. Since we reverse the second half of the linked list we can just call while(mid) on the second while loop cuz mid will point to null eventually.
/**
* 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}
*/
const reverseList = head =>{
let cur = head;
let prev = null;
let next = null;
while(cur){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
var isPalindrome = function(head) {
let slow = head;
let fast = head;
// fast.next cuz we use fast.next.next and that would return an error if fast.next is null
while(fast && fast.next){
fast = fast.next.next;
slow = slow.next;
}
let mid = reverseList(slow);
let pointer = head;
while(mid){
if(mid.val !== pointer.val){
return false
}
mid = mid.next;
pointer = pointer.next;
}
return true;
};
Thanks for this awesome video; and all the other videos! This helps me so much.
Amazingly explained. Thanks for sharing!
Great video!!!
how can leetcode regard this as easy
another vid of gold