Reverse Linked List II - Leetcode 92 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ม.ค. 2025

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

  • @NeetCode
    @NeetCode  3 ปีที่แล้ว +10

    💡 LINKED LIST PLAYLIST: th-cam.com/video/G0_I-ZF0S38/w-d-xo.html

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

      thanks

    • @洪豐雄
      @洪豐雄 3 ปีที่แล้ว

      Thanks for your explanation, it helps me a lot !!!

    • @jakubucinski
      @jakubucinski 4 หลายเดือนก่อน

      Hey, for visual problems like this would you consider Leetdebug - th-cam.com/video/lXWgPCYDfMc/w-d-xo.html as being something worth integrating in NeetCode ? I have gotten some good feedback on this project from people - but integrating it in NeetCode would be a dream come true as it could potentially help the community quite a lot. What do you think ?

  • @shelllu6888
    @shelllu6888 ปีที่แล้ว +146

    For those who find this problem chanllenging, you are not alone. This is still by far the best explanation for this problem. Highly encourage you to hang in there, pause the video multiple times, understand it, take a break and come back to it. It will eventually make sense, as long as you don't give up and show up to understand it.

    • @memesbyanony
      @memesbyanony ปีที่แล้ว

      bro i m confused : if in case left is at 1st index then dummy.next points to head i.e. first node but answer would be right if leftprev.next is returning

    • @chrischika7026
      @chrischika7026 6 หลายเดือนก่อน +1

      came back after sometime and I understand it fully.

    • @saishivamgupta4584
      @saishivamgupta4584 4 หลายเดือนก่อน +1

      @@memesbyanony it's literally explained at 05:23

    • @John-ds4ce
      @John-ds4ce 3 หลายเดือนก่อน

      i thought of the same idea but wasnt able to write a code properly as i got really confused, the solutions i tried to read on leetcode confused me even more

  • @mashab9129
    @mashab9129 3 ปีที่แล้ว +45

    coding it piece by piece after each illustrated/explained block was a great idea. thank you!

  • @dabaaku
    @dabaaku 7 หลายเดือนก่อน +1

    Your explanations on these problems helped me a lot! Thanks for all the hard work that went into making these videos.

  • @AmrElmohtaseb
    @AmrElmohtaseb 11 หลายเดือนก่อน +3

    It brings me an immense relief whenever I'm facing an issue understanding a LeetCode problem and I find you have a video for it.

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

    I like this style of explaining where you go back and forth between the drawing and the code. It helps me mentally map the concept directly to implementation

  • @iugaialeksei2108
    @iugaialeksei2108 ปีที่แล้ว

    Dude, I have nothing but admiration for how you approach various problems. Every time you do it clear, simple and smooth.

  • @pingpangqiu5605
    @pingpangqiu5605 2 ปีที่แล้ว +3

    Very good illustration and idea. I feel I will never be able to solve this within a normal coding interview session by myself

  • @mayank3411
    @mayank3411 3 วันที่ผ่านมา

    for anyone having issue in this solution try first solve reverse linked list 1 and then come up with this to solve...It is really easy to understand when you know the previous question..and Thanks once again to Neetcode for helping me out.

  • @dheepthaaanand3214
    @dheepthaaanand3214 หลายเดือนก่อน

    Very clever handling of the edge cases:
    1. If we are reversing the last n nodes, the last node after reversing will point to null as expected as cur would be null
    2. If we are reversing the first n nodes, we have a leftPrev node thanks to dummy. leftPrev.next would point to the right node and when we return dummy.next that is essentially the right node

  • @shinewbiez
    @shinewbiez 2 ปีที่แล้ว +4

    Coding while explaining was a great idea and more understandable, kindly follow the same pattern for further videos!

  • @MP-ny3ep
    @MP-ny3ep ปีที่แล้ว +1

    Breaking it into 3 parts helped a lot. Thank you

  • @jadkhalil9263
    @jadkhalil9263 2 ปีที่แล้ว +3

    Great video only thing that got me was the edge cases. Very clever to use the dummy node, You could say it was some NeetCode.

  • @carloscarrillo201
    @carloscarrillo201 2 ปีที่แล้ว +2

    You really deserve heaven, good man! Thanks so much for doing this. A donation is totally well deserved!

  • @MohSo14
    @MohSo14 หลายเดือนก่อน

    Great explanation as always. Wonderful skills to break down the problem and starting to solve it!

  • @quirkyquester
    @quirkyquester 2 ปีที่แล้ว +5

    great explanation! this problem seems easy, but it's really tricky

  • @triscuit5103
    @triscuit5103 3 ปีที่แล้ว +13

    ollie mollie man, this is such a great explanation, you've got such a talent to convey information. Congrats on the job, well deserved. I myself got interviews lined up pretty much with all of FAANG and your videos are my go-to source when I want to just sit and watch something rather than practice hands-on coding.

    • @leeroymlg4692
      @leeroymlg4692 ปีที่แล้ว +2

      did you get the job?

    • @triscuit5103
      @triscuit5103 ปีที่แล้ว

      @@leeroymlg4692 yeah I did

  • @dennisgray8199
    @dennisgray8199 8 หลายเดือนก่อน

    Another way to think about the very first part of this algorithm is: "Until current.val is L, move the prev/curr pointers up". I found this to be a more intuitive way of thinking than the (left - 1) approach here (YMMV).
    Similarly, in the second phase of the algorithm, instead of thinking about indicies you can just think about it like this: "Reverse the list, once prev.val = R you, you know you're done reversing the sublist."
    I find the less I can think about indicies the more high-level I can think about the algorithm.

  • @tsunghan_yu
    @tsunghan_yu 2 ปีที่แล้ว +2

    More readable code:
    # Definition for singly-linked list.
    # class ListNode:
    # def __init__(self, val=0, next=None):
    # self.val = val
    # self.next = next
    class Solution:
    def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
    dummy = ListNode()
    dummy.next = head
    prev, cur = dummy, head
    for _ in range(left - 1):
    prev = cur
    cur = cur.next
    left_prev, left_node = prev, cur
    prev, cur = None, left_node
    for _ in range(right - left + 1):
    tmp = cur.next
    cur.next = prev
    prev = cur
    cur = tmp
    right_node, right_next = prev, cur
    left_prev.next = right_node
    left_node.next = right_next
    return dummy.next

  • @maamounhajnajeeb209
    @maamounhajnajeeb209 2 ปีที่แล้ว +3

    you motivate me to solve problem everyday man

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

      Keep up the good work 💪

  • @abhishekkulkarni1702
    @abhishekkulkarni1702 11 หลายเดือนก่อน

    I am simply amazed by the clear explanation! Just simply wow...Thank you so much

  • @algosavage7057
    @algosavage7057 3 ปีที่แล้ว +5

    you are awesome, dude. Thanks for such clear explanations!

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

      Thanks, happy it's helpful!

  • @AshwaniSharma-of2nq
    @AshwaniSharma-of2nq 9 หลายเดือนก่อน

    Amazing explanation again, that trick of adding dummy node really helped.

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

    Best video explanation for it... thnx for the effort 😇

  • @konradhunter1407
    @konradhunter1407 6 หลายเดือนก่อน

    Great code, as usual! Commenting the code like this is also helpful.

  • @sheikhmkrifat7749
    @sheikhmkrifat7749 9 หลายเดือนก่อน

    Thanks for such clear explanations! Love from Bangladesh

  • @winepim
    @winepim 6 หลายเดือนก่อน

    Thanks!

  • @oneeyedking3894
    @oneeyedking3894 26 วันที่ผ่านมา

    brooo i did it on my own using recursion and it works... i am finally seeing some progress 😭😭😭😭😭

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

    I had solution idea like that, but could not implement properly. So thanks very much for that.

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

    Thank you for the explanation! However, for 1 test case the given list is [1,2,3,4] 1,4
    my output is [4,2,3,1], but the expected output is [4,3,2,1]
    Could you please tell me where I'm lacking? I'm using two pointer method in python

  • @aynuayex
    @aynuayex ปีที่แล้ว

    really helpful man, you are motivating me to work on more problems everyday thanks a million.
    but can you come up with the Follow up: Could you do it in one pass?

  • @quanvu2579
    @quanvu2579 2 หลายเดือนก่อน

    thanks for explaining!

  • @krishankantray
    @krishankantray 8 หลายเดือนก่อน +1

    It looks easy but when it comes to write a compilable code in interview there are good changes of making mistake.

  • @shantipriya370
    @shantipriya370 7 หลายเดือนก่อน

    wonderful explanation

  • @Cloud-577
    @Cloud-577 3 ปีที่แล้ว

    thank you soo too much this I much cleaner and easy to understand and probably will never forget

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

    Those who can reverse a Linked List, they can easily solve this.

  • @ARkhan-xw8ud
    @ARkhan-xw8ud 6 หลายเดือนก่อน

    Thanks for this bro

  • @usamahussain4461
    @usamahussain4461 3 หลายเดือนก่อน

    bro you're so smart!

  • @baolingchen2891
    @baolingchen2891 2 ปีที่แล้ว +1

    I use Java but this is very helpful! Thank you!

    • @anushab.v.8512
      @anushab.v.8512 2 ปีที่แล้ว +2

      Here is my Java solution, it's failing for [5] 1 1 . How do you handle this case?
      public ListNode reverseBetween(ListNode head, int left, int right) {
      // 0. ass dummy node at start this helps in hadling edge cases
      // if left is head itself etc.
      ListNode dummy = new ListNode(0, head);
      // dummy.next = head;
      // 1. Iterate till you find leftNode, remeber prevLeft (iterate left-1)
      ListNode leftPrev = dummy;
      ListNode curr = head;
      while(curr!=null && curr.val

    • @__LAmar__
      @__LAmar__ ปีที่แล้ว

      ​@@anushab.v.8512 Guess I'm really late, but why are you checking for value? Value of node has nothing to be with anything here. Maybe that's why you are failing the test case

  • @begula_chan
    @begula_chan 10 หลายเดือนก่อน

    Thanks bro very much!

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

    Awesome man!!! Thanks for this.....

  • @sushantgupta-lx2nv
    @sushantgupta-lx2nv ปีที่แล้ว

    Thank you so much sir

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

    Great video!

  • @sushmitgaur8537
    @sushmitgaur8537 ปีที่แล้ว

    if r = 4 then shouldn't the element at 4th index be 5? 'cuz the index starts from 0?

    • @namanbansal8613
      @namanbansal8613 8 หลายเดือนก่อน

      Given in the question that "l" refers to 2nd index, implying index reference as 1 being the first node.

  • @akash5653
    @akash5653 ปีที่แล้ว +4

    Linked List problems aren't hard but they're confusing af

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

    Great video, thanks!

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

    Thanks for the clean illustration. ^_^

  • @amberfatima2777
    @amberfatima2777 ปีที่แล้ว

    best solution😌

  • @faaeizkhan6134
    @faaeizkhan6134 3 หลายเดือนก่อน

    i used stack for this problem

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

    Thanks man

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

    Hey can you please do 'create BSt from levelorder array' problem.

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

    thanks ! :D

  • @SamarAshrafii
    @SamarAshrafii 4 หลายเดือนก่อน

    Does anyone know how I can fix the below code, I would appreciate help. I tried to solve it based on the solution for reverse node in K group.
    dummy = ListNode(0, head)

    # Get nodes
    left_node, prevlef = self.getKth(left, dummy)
    right_node, _ = self.getKth(right, dummy)

    # Reverse the segment
    prev, curr = None, left_node
    while curr != right_node.next:
    tmp = curr.next
    curr.next = prev
    prev = curr
    curr = tmp

    # Connect the reversed segment back into the list
    prevlef.next = prev
    left_node.next = right_node.next
    return dummy.next
    def getKth(self, k, curr):
    prev = None
    while k > 0:
    prev = curr
    curr = curr.next
    k -= 1
    return curr, prev

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

    so smooth!!!

  • @abhishekkrishna5978
    @abhishekkrishna5978 10 หลายเดือนก่อน

    whatif i use a arraylist to swap

  • @CST1992
    @CST1992 8 หลายเดือนก่อน

    This code is way harder to write than what appears at the end of this video.

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

    This problem is more obnoxious than it is challenging

  • @jey_n_code
    @jey_n_code ปีที่แล้ว

    FIRST FIND AND UNDERSTAND HOW TO REVERSE A LINKEDLIST IN-PLACE, AFTER IT WILL BECOME WAY MORE EASIERR PROMISEE

  • @Linuxgnuorgme
    @Linuxgnuorgme 3 หลายเดือนก่อน

    Time execution is slow

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

    Thanks for great explanation. Just want to give one feedback - while explaining you are writing too many color markings which can be avoided. second - coding can be written much understandable format for other language people. you writing deep python code like merging multiple statements into one.

    • @triscuit5103
      @triscuit5103 3 ปีที่แล้ว +11

      Well, that's a very subjective thing. Here is my feedback:
      Color markings are great, and really help to visualise the solution. Different colors for pointers, values, nodes, edges between nodes, it makes everything so much easier to follow and understand.
      I don't have Python in my arsenal, but I love it for the purposes of these tutorials. I can clearly understand each line of code, there is no black magic in there, and it would much more verbose in most other languages, which would take more time and potentially confuse viewers.

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

    simpler solution
    class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
    slow,fast = head,head
    meet = head
    while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
    if slow == fast:
    break
    else:
    return None
    while meet != slow:
    slow = slow.next
    meet = meet.next
    return meet

    • @tsunghan_yu
      @tsunghan_yu 2 ปีที่แล้ว +3

      Did you post in the wrong video?

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

      Floyd's algo serves a diff purpose. i think u posted this in the wrong video mate.

    • @chrischika7026
      @chrischika7026 6 หลายเดือนก่อน

      tf

  • @hanif3661
    @hanif3661 2 ปีที่แล้ว +1

    ⌘⌘ AMAZING ⌘⌘