Palindrome Linked List (LeetCode 234) | Full solution with trick | Study Algorithms

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • A sequence could be represented using a string, array or even a linked list. A given list will be a palindrome if we get the same order when traversing from head to null, and null to head. This can be done by using a stack data structure and if space is an issue, we can also modify the actual list to determine the palindrome.
    Actual problem on LeetCode: leetcode.com/problems/palindr...
    Chapters:
    00:00 - Intro
    01:03 - Problem Statement and Description
    02:37 - Solution using Stacks
    07:24 - Space Optimized Solution
    10:49 - Dry-run of Code
    13:24 - Final Thoughts
    📚 Links to topics I talk about in the video:
    Linked List: • Linked List Data Struc...
    Traversing Linked List: • Traversing a Linked Li...
    Reverse a Linked List: • HackerRank - Reverse L...
    Stack Data Structure: • Stack Data Structure e...
    📘 A text based explanation is available at: studyalgorithms.com
    Code on Github: github.com/nikoo28/java-solut...
    Test-cases on Github: github.com/nikoo28/java-solut...
    📖 Reference Books:
    Starting Learn to Code: amzn.to/36pU0JO
    Favorite book to understand algorithms: amzn.to/39w3YLS
    Favorite book for data structures: amzn.to/3oAVBTk
    Get started for interview preparation: amzn.to/39ysbkJ
    🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
    🎥 My Recording Gear:
    Recording Light: amzn.to/3pAqh8O
    Microphone: amzn.to/2MCX7qU
    Recording Camera: amzn.to/3alg9Ky
    Tablet to sketch and draw: amzn.to/3pM6Bi4
    Surface Pen: amzn.to/3pv6tTs
    Laptop to edit videos: amzn.to/2LYpMqn
    💻 Get Social 💻
    Follow on Facebook at: / studyalgos
    Follow on Twitter at: / studyalgorithms
    Follow on Tumblr at: / studyalgos
    Subscribe to RSS feeds: studyalgorithms.com/feed/
    Join fan mail: eepurl.com/g9Dadv
    #leetcode #programming #interview

Komentáře • 30

  • @matthewzarate8851
    @matthewzarate8851 Před 2 měsíci

    Loved the explanation for both implementations! Thanks Nikhil!

  • @user-hn3jj7lu7c
    @user-hn3jj7lu7c Před 10 měsíci

    such a clean explanation, i loved it

  • @sameerkumarsingh8942
    @sameerkumarsingh8942 Před 7 měsíci

    i dont but i get a lot of clarity from your videos , Thanks a lot

  • @poojakasar4572
    @poojakasar4572 Před 11 měsíci +3

    explanation is awesome💥💥👍

  • @hunorvadasz-perhat6001
    @hunorvadasz-perhat6001 Před rokem +3

    Good explanation! Thank you for the great video 😃

    • @nikoo28
      @nikoo28  Před rokem +1

      thank you for subscribing.. :)

  • @bhumikabansal6022
    @bhumikabansal6022 Před 10 měsíci

    your all explanations are awesome

    • @nikoo28
      @nikoo28  Před 10 měsíci

      Thank you so much 😀

  • @AlgoXperience
    @AlgoXperience Před 8 měsíci +1

    i love it

  • @infinite639
    @infinite639 Před rokem +1

    Why not using collection linkedlist of java
    Why we are making our own linkedlist ?
    Why we are making our own tree?
    Please answer

  • @deeptigarg799
    @deeptigarg799 Před 4 měsíci

    When we are doing fast = head and traversed fast to null then why head is not null. Aren't we dealing with the address?

  • @padmajab2294
    @padmajab2294 Před rokem

    Nice explanation

  • @honey-xr5kp
    @honey-xr5kp Před 4 měsíci

    Love your videos! Very well explained. When I tried this code though, I received a time limit exceeded error. I'm sure its on my end, but I'm not sure why because as far as I know its the same code and should work.

    • @nikoo28
      @nikoo28  Před 4 měsíci

      try the code available on github (link in description)

  • @121sarthkumar4
    @121sarthkumar4 Před 3 měsíci

    when we do fast= head does it start from for head of ex 237732 this value or is it half

    • @nikoo28
      @nikoo28  Před měsícem

      Can you please elaborate on your example?

  • @shubhamgupta8248
    @shubhamgupta8248 Před 11 měsíci +2

    Not able to understand why i am getting an error when i write
    while(fast!=null)
    {
    if(fast.val!=slow.val)
    {
    return false;
    }

    • @nikoo28
      @nikoo28  Před 10 měsíci

      compare to the code I have

    • @naruto_6663
      @naruto_6663 Před 5 měsíci +1

      so if this answer matters
      if we write fast != null we will see we have not breaked the linked list in two there is still connection between the 7->2 so that will not be null thats why we write slow != null as slow pointer will point to null 7->null (after the reverse of the list):)

  • @indrajith2002
    @indrajith2002 Před 6 měsíci

    bro but for odd testcase it doesnt work .lets say we have a testcase 1->2->3->2->1->null according to you slow points to middle 3 ,
    now if we reverse it will become 1->2->3->null ,where fast points to starting 1 .now if start comparing values @ fast and slow
    upto 2 it will be same after that slow points to 3 but fast points to null .so what to do?

    • @nikoo28
      @nikoo28  Před 6 měsíci

      It does work, did you try with such a case?

  • @cautioni
    @cautioni Před 3 měsíci

    please do a longest palindrome in a linked list

    • @nikoo28
      @nikoo28  Před 3 měsíci

      Will add it to my pipeline of video

  • @rezowanamehjabinlorel1428
    @rezowanamehjabinlorel1428 Před 4 měsíci

    Please make more explanation videos

    • @nikoo28
      @nikoo28  Před 4 měsíci +1

      Will do..new video every week 😄
      Please share as much possible too 😎

  • @appikeeru5785
    @appikeeru5785 Před 13 dny

    Y in his videos very less viewers😢

  • @alihaiderzada423
    @alihaiderzada423 Před rokem +3

    The stack solution does not work if the pattern is 2 - 3 - 7 - 3 - 2. even tho it is a palindrome

    • @bhumikabansal6022
      @bhumikabansal6022 Před 10 měsíci +1

      go with the optimized one

    • @SaumyaSharma007
      @SaumyaSharma007 Před 10 měsíci +2

      Hello, what you can do:
      Traverse entire L. List. (Let Length of LList come out be n)
      Just push n/2 elements in the stack.
      Now you can check if n is odd which is your case. Curr will be pointing to -7.
      So Curr = Curr-> next;
      Now start comparing left-out L list with stack elements.
      If n was even. Eg 2 -3 -3 2
      Then We would have done nothing. Coz We have already pushed n/2 elements. And Curr will be pointing to the Third element. Now start comparing left-out L list with stack elements.
      Hope it helps. 😄

    • @alihaiderzada423
      @alihaiderzada423 Před 10 měsíci

      @@SaumyaSharma007 awesome thanks buddy i will give it ago.. :)