Even for odd linkedlist size, this will not happen because let’s say we have an example like 2=>1=>0, after we get the slow pointer points to the middle node which is Node(1), we will reverse the last half of the linked list. So we get something like this 2=>1
Even for odd linkedlist size, this will not happen because let’s say we have an example like 2=>1=>0, after we get the slow pointer points to the middle node which is Node(1), we will reverse the last half of the linked list. So we get something like this 2=>1
oh lord,I took queue data structure to put given list into queue, then i reversed the list and iterated through each node checking if val are equal or not.
Your approach will give us O(n) space complexity because we need a queue to store those nodes, and my approach in the video only uses O(1) space complexity because I only use pointers.
It will not fail (the code is in the video description, you can run it) because after the slow pointer points to Node(0) (the middle node) we reverse the last half of the linkedlist, we get the Node(1) (on the right) points to Node(0) and we also have Node(1) (on the left) also points to Node(0). So when we comparing the node's val, it will be the same. Therefore, 1=>0=>1 will not fail.
Thanks for uploading .
I am very glad that it was helpful 😊
and what about the linked list which are of odd length like 1-2-1.
should't it be while(fast!=slow && slow!=null) on line 29????
Even for odd linkedlist size, this will not happen because let’s say we have an example like 2=>1=>0, after we get the slow pointer points to the middle node which is Node(1), we will reverse the last half of the linked list. So we get something like this 2=>1
@@EricProgramming Great explaination! 2-->1
Nice things to check Palindrome. Can you give a solution to check is there any Palindrome date?
What kind of palindrome date?
it keeps saying Line 2: Char 11: error: expected ':'
public boolean isPalindrome(ListNode head) {
^
:
how do we fix that
odd number of elements then we need to increase slow = slow.next right? before we reverse
Even for odd linkedlist size, this will not happen because let’s say we have an example like 2=>1=>0, after we get the slow pointer points to the middle node which is Node(1), we will reverse the last half of the linked list. So we get something like this 2=>1
oh lord,I took queue data structure to put given list into queue, then i reversed the list and iterated through each node checking if val are equal or not.
Your approach will give us O(n) space complexity because we need a queue to store those nodes, and my approach in the video only uses O(1) space complexity because I only use pointers.
its failing for 1->0->1 😥
It will not fail (the code is in the video description, you can run it) because after the slow pointer points to Node(0) (the middle node) we reverse the last half of the linkedlist, we get the Node(1) (on the right) points to Node(0) and we also have Node(1) (on the left) also points to Node(0). So when we comparing the node's val, it will be the same. Therefore, 1=>0=>1 will not fail.