[Java] Leetcode 234. Palindrome Linked List [Fast & Slow Pointers #4]

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 พ.ย. 2024

ความคิดเห็น • 14

  • @kannathamao9265
    @kannathamao9265 3 ปีที่แล้ว +1

    Thanks for uploading .

    • @EricProgramming
      @EricProgramming  3 ปีที่แล้ว

      I am very glad that it was helpful 😊

  • @TahliaYeseniaJones
    @TahliaYeseniaJones 3 ปีที่แล้ว +1

    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????

    • @EricProgramming
      @EricProgramming  3 ปีที่แล้ว

      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

    • @MeetManga
      @MeetManga 2 ปีที่แล้ว

      @@EricProgramming Great explaination! 2-->1

  • @jkltest5090
    @jkltest5090 3 ปีที่แล้ว +1

    Nice things to check Palindrome. Can you give a solution to check is there any Palindrome date?

  • @EditsbyA
    @EditsbyA 2 ปีที่แล้ว

    it keeps saying Line 2: Char 11: error: expected ':'
    public boolean isPalindrome(ListNode head) {
    ^
    :
    how do we fix that

  • @VirginiaAdams763
    @VirginiaAdams763 3 ปีที่แล้ว +1

    odd number of elements then we need to increase slow = slow.next right? before we reverse

    • @EricProgramming
      @EricProgramming  3 ปีที่แล้ว

      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

  • @Joseph45758
    @Joseph45758 3 ปีที่แล้ว +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.

    • @EricProgramming
      @EricProgramming  3 ปีที่แล้ว

      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.

  • @eleanor8967
    @eleanor8967 3 ปีที่แล้ว +1

    its failing for 1->0->1 😥

    • @EricProgramming
      @EricProgramming  3 ปีที่แล้ว

      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.