Reverse Linked List - Iterative AND Recursive - Leetcode 206 - Python

  Рет қаралды 384,165

NeetCode

NeetCode

Күн бұрын

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Coding Solutions: • Coding Interview Solut...
Problem Link: neetcode.io/problems/reverse-...
0:00 - Read the problem
0:50 - Drawing Iterative
3:00 - Coding Iterative
4:48 - Drawing Recursive
8:15 - Coding Recursive
leetcode 206

Пікірлер: 180
@NeetCode
@NeetCode 3 жыл бұрын
🚀 neetcode.io/ - A better way to prepare for Coding Interviews
@enriquewilliams8676
@enriquewilliams8676 2 жыл бұрын
Can you tell me in which occasion does reverse linked list are used...?
@XDMico
@XDMico Жыл бұрын
@@enriquewilliams8676 Coding interviews
@Extremesarova
@Extremesarova 2 жыл бұрын
Maybe this recursive solution can make things clearer for you, guys def reverseListRecursive(self, head): def reverse(cur, prev): if cur is None: return prev else: next = cur.next cur.next = prev return reverse(next, cur) return reverse(head, None) Also, I think that we are using three pointers here: prev, cur and next.
@zzpdanny
@zzpdanny 2 жыл бұрын
Yooo this solution is fire.Thank you so much dude
@arkamukherjee457
@arkamukherjee457 2 жыл бұрын
Amazing solution!
@dhirendrasingh6071
@dhirendrasingh6071 2 жыл бұрын
Uses iterative code lines, nice
@anishsubedi6506
@anishsubedi6506 Жыл бұрын
Easiest recursive to understand.
@batnasn
@batnasn Жыл бұрын
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: def reverse(prev, curr): if not curr: return prev tmp = curr.next curr.next = prev return reverse(curr, tmp) return reverse(None, head)
@mobaisi13
@mobaisi13 2 жыл бұрын
Unfortunately the recursive code doesn't make much sense. Pretty much all the variants in the comments are much easier to digest. The iterative solution was great though!
@usnaveen
@usnaveen Жыл бұрын
Thank you ✌️
@DukensteinA1
@DukensteinA1 Жыл бұрын
Interestingly, the recursive function made a million times more sense to me.
@detroitpistonsplayoffs
@detroitpistonsplayoffs 11 ай бұрын
Yea it's honestly making me feel like an idiot
@manishmaharjann
@manishmaharjann 2 жыл бұрын
I have struggled so much on this simple problem. You explained it so simply. You are genius thanks for that !
@joshbouganim7423
@joshbouganim7423 2 жыл бұрын
Passing in a prev node to the recurse call makes the logic much more easier to grasp and mimics the logic of the iterative solution. public ListNode ReverseList(ListNode head) { return Recurse(null, head); } public ListNode Recurse(ListNode prev, ListNode cur){ if(cur == null) return prev; //save new next node var newNext = cur.next; //reverse cur.next = prev; //reverse rest of the list return Recurse(cur, newNext); }
@MistaT44
@MistaT44 2 жыл бұрын
That’s much easier to digest! Thanks for sharing
@anthonypark9158
@anthonypark9158 2 жыл бұрын
After spending a long time to understand Neetcode's recursion, yours took like 1 min to understand. thanks for sharing another way!
@ytwatcher181
@ytwatcher181 Жыл бұрын
Thanks, this comment is worth pining
@jxswxnth
@jxswxnth Жыл бұрын
Yeah it's recursive but it's nothing but changing the same iterative loop in a recursive way. This solution not certainly using dfs as the solution in this video.
@amlivinginhell
@amlivinginhell 9 ай бұрын
i lost hope in understanding the recursive solution and then saw your comment. thank you Josh,
@jagggyjazz8010
@jagggyjazz8010 Жыл бұрын
Bro cleared my confusion in an instant, got the logic but I made a silly mistake of things prev as something similar to curr.nxt, but it is similar to the temp variable nxt. Was scratching my head to interchange 1 line of code. I love this.
@Inconvenient-MRKim
@Inconvenient-MRKim Жыл бұрын
Dude! Thank you so much. I'm preping for my interviews through your 150 questions and was struggling with this basic idea of reversing for a long time. You making videos of even such easy questions and concepts helps a lot. I really appreciate all your help and efforts taken for making the website and videos.
@Mr.Fantomblox
@Mr.Fantomblox Жыл бұрын
Can I ask how your interview went?
@Inconvenient-MRKim
@Inconvenient-MRKim Жыл бұрын
@@Mr.Fantomblox It was good. Luckily they asked 2 easy questions. String reverse and average of top student from a list of marks. Solved them, got a reject due to hiring freeze. Good experience though!
@upsidedownChad
@upsidedownChad 5 ай бұрын
@@Inconvenient-MRKim May I ask if this was a faang company?
@Inconvenient-MRKim
@Inconvenient-MRKim 5 ай бұрын
Yes it was. Most questions come from neetcode or are similar in patterns. I couldn’t clear the interview due to my lack of skills. So working for a faang still remains a goal to achieve
@shatakshivishnoi916
@shatakshivishnoi916 Жыл бұрын
your tutorials help a lot..thankyou!!
@rajenderkatkuri7642
@rajenderkatkuri7642 Жыл бұрын
You make it seem effortless. ❤️
@ax5344
@ax5344 3 жыл бұрын
i still struggle with the recursion code ... i can understand the logic, but not the code... sigh. Thanks for sharing!
@tanayshah275
@tanayshah275 3 жыл бұрын
try to understand this you might get this code: def reverseList(self, head: ListNode) -> ListNode: if head == None or head.next == None: return head newHead = self.reverseList(head.next) head.next.next = head head.next = None return newHead
@yhdzhang
@yhdzhang 2 жыл бұрын
I had the same problem when I started LC. Recursion code never made any sense to me. (I'd spend hours trying to understand solutions) You just got to keep brute forcing recursion code and problems and eventually it "clicks"
@orangethemeow
@orangethemeow 2 жыл бұрын
@@tanayshah275 I'm having issue understanding head.next.next = head
@rockstars369
@rockstars369 2 жыл бұрын
@@orangethemeow On the way back from recursive calls.... its a quick way of accessing the end-node of the new reversed linked list and make that end-node's next point to the node(head) in the current recursion frame. Not sure if that makes sense... hope below example makes it clear: Input HEAD: 1->2->3->null At the end of recursive calls 3 becomes newHead and it returns from last recursive call... now we have two ways to make 3 point to 2, I will put node value in {} for clarity : A) newHead{3}.next = head{2} OR B) head{2}.next{3}.next = head{2} Now, down the line when it is time for pointing 2 to 1: newHead still points to 3 but we can point node 2 to 1 by using head{1}.next{2}.next = head{1} even though option A worked in the first case, option B will always work down the line as newHead always points to first element in the reversed list i.e 3 in this case.
@dnguyendev
@dnguyendev 2 жыл бұрын
@@rockstars369 Thank you! The 'head.next.next' confused the heck out of me. Putting number next to it makes it way easier to understand!
@the-tankeur1982
@the-tankeur1982 11 ай бұрын
Every time i'm amazed by how much you can simplify solutions and how well you can explain thank you for all that
@KSMuralidhar
@KSMuralidhar 8 ай бұрын
Nice answer . multiple new_head assignment is bit confusing. practically, if head is null(in case of empty list) or head.next is null will marked as root. so we can easily stop recursion at last node and keep reversing list without much confusion. this approach has similar time and space requirements(in terms of leet code as well as big oh. infact it's quite better). -------------------------- code below --------------------------- public ListNode reverseList(ListNode head) { if(head == null || head.next == null ) return head; ListNode newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } -------------------------------------------------------------------------
@sujalkumarsinha
@sujalkumarsinha 8 ай бұрын
I was returning curr when doing this on my own and kept wondering why it won't work when I should've returned prev smh. Thanks for explaining this in an easy manner .
@2NormalHuman
@2NormalHuman 2 жыл бұрын
Really good explanation! Thank you
@user-fk1wo2ys3b
@user-fk1wo2ys3b Жыл бұрын
HUGE THANKS!
@eshaanmehta9537
@eshaanmehta9537 8 ай бұрын
Would another valid solution be to create a new head node. And continuously pop the first node from the original list and push front to the new list until the original head is null. Then return the new head. Wouldn’t this also have a TC of O(n) and SC of O(1)? Please if I am mistaken I appreciate any corrections, I am new to learning data structures. Thanks!
@osmanmusse9432
@osmanmusse9432 2 жыл бұрын
Great Explanation
@bruce716
@bruce716 Жыл бұрын
Hope this is helpful. def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: # Edge case, in case input head is None, that is 0 node. if not head: return None # Both base case and edge case. # Edge case, if input head has 1 Node only # Base case, because the last node does not have next Node, the last node will return self as lastNode. lastNode = head # Recursive case if head.next: # This line always return the last node no matter which stack you are at. lastNode = self.reverseList(head.next) # This is more like 1 -> 2 -> None # turn into >>> 1 -> 2 -> 1, now the Node(2).next points to Node(1) which is reversed now head.next.next = head # For line above, in case there is a cycle head.next = None # for example # 1 > 2 > 3, every stack return Node(3) # 1 > 2, every stack return Node(2) return lastNode
@notphilly309
@notphilly309 10 ай бұрын
bro your explanation helped a lot thanks man
@JimmyCheng
@JimmyCheng Жыл бұрын
the recursive solution is mind-buggling but it is the essence of a recursive funciton, basically throw the problem at the computer and let it do the work
@alr0495
@alr0495 4 ай бұрын
Was entirely confused at first as well. He say says in the video that the "if not head" statement is for the base case, but it really is only for the edge case where you're given an empty list (I ran it through some sample solutions on leetcode and all other test cases worked). The real base case is simply evaluated through the "if head.next" part, which will cut subsequent recursive calls if you reach the end of the list i.e. head.next is evaluated as null (or None in Python)
@100bands
@100bands 11 ай бұрын
Thanks!
@mrkandreev
@mrkandreev 3 ай бұрын
It is much more easier to use one line assignment: curr.next, prev, curr = prev, curr, curr.next
@sanooosai
@sanooosai Жыл бұрын
thank you sir
@shreerambhat1442
@shreerambhat1442 2 ай бұрын
or another solution(but not the most optimal) is to use stack. as we traverse to the end we can store it in stack ,and then use it.i.e in loop till stack is not empty: currenthead.next =stack.pop() currenthead=currenthead.next
@pinakadhara7650
@pinakadhara7650 Жыл бұрын
Awesome explanation
@NeetCode
@NeetCode Жыл бұрын
Glad it was helpful!
@EatingGuo
@EatingGuo 2 жыл бұрын
can someone tell me how does it make the nodes after first node reverse by using newhead = reverselist(self.next). i dont see body in the def reverselist. author didnt explain this part.
@ashishchoudhary1664
@ashishchoudhary1664 2 ай бұрын
Line 16: head.next.next is the one reversing the nodes. It is more like saying point the NEXT to NEXT node to myself. Which means asking the pointer of current head's next node to point to the current head itself, thereby reversing the next node. This would produce a cycle of pointer therefore to break the cycle head.next is set to None in next line.
@alexandersmirnov4274
@alexandersmirnov4274 5 ай бұрын
that was helpful
@yevhendubinin
@yevhendubinin 6 ай бұрын
Looks like in recursive call the last call in the stack (the one at the bottom with reverseList(1) receives (new) reversed head pointer, and tail pointer. input: head->1->2->3 --stack top reverseList(3) -> 3->nil reverseList(2) -> 3->2->nil reverseList(1) -> 3->2->1->nil --- stack bottom zooming in to the call "reverseList(1)" here: func reverseList(_ head: ListNode?) -> ListNode? { // "head" holds ref to "1" var newHead = head if head?.next != nil { newHead = reverseList(head?.next) // 'newHead->3-2 ; head?.next->2 (notice that 2 is our tail) head?.next?.next = head // set 1 to tail ''newHead->3-2->1 head?.next = nil // it removes the cycle, looks like 2->2 ??? } return newHead }
@The88bigchief
@The88bigchief Жыл бұрын
what about this? def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: c = head while c and c.next: n = c.next c.next = n.next n.next = head head = n return head
@Mamtagoyal27
@Mamtagoyal27 2 жыл бұрын
@NeetCode, Thanks for putting this video but I think Line #13: newHead = Head is not required at all.
@thebiglongshlong
@thebiglongshlong 2 жыл бұрын
goat talk!
@narkotr4fic843
@narkotr4fic843 Жыл бұрын
when we assign to newHead variable recursive call function (line 15)- will it automatically start new recursive call? or it will skip this line to next - 16 line?
@skatlag
@skatlag 5 ай бұрын
it will keep starting new recursive calls until it hits the base case (if head.next is None) at which point it'll execute line 17 (assigning head.next to None) and will execute line 19 (returning newHead)
@bulioh
@bulioh Жыл бұрын
renamed variables + added comments to the recursive solution to make things clearer for myself/hopefully others: def reverseList(self, curr): if not curr: # this is ONLY for if the original test input is empty, doesn't apply in all other cases return None oldTail = curr # keep assigning to curr as we recurse down, then the last one will be the one we actually pass all the way back up if curr.next: oldTail = self.reverseList(curr.next) # stays the same as we recurse back up curr.next.next = curr # this is what actually points next back to curr as we recurse back up curr.next = None # point to None in case we've recursed back up to the old head (new tail now) return oldTail # keep passing all the way up to eventually be the new head
@brandonwilcox3307
@brandonwilcox3307 2 жыл бұрын
Thank you for this explanation. I have a lot of difficulty visualizing recursive problems so this was very helpful. Though for your iterative solution, wouldn't that technically be a 3 pointer solution due to the addition of the next variable?
@potatocoder5090
@potatocoder5090 2 жыл бұрын
yeah.
@vivekbinjola2460
@vivekbinjola2460 Жыл бұрын
helpful
@jamesdouthit7489
@jamesdouthit7489 3 ай бұрын
I have seen a couple shorter ways to do it than neetcode. This is the shortest and most intelligible def reverseList(self, head: Optional[ListNode], prev=None) -> Optional[ListNode]: if head is None: return prev nxt = head.next head.next = prev return self.reverseList(nxt, head)
@prikshitbatta
@prikshitbatta Жыл бұрын
why are you returning prev in two pointer approach?
@andrewvirtual
@andrewvirtual Жыл бұрын
Explained it much better than Geeks for Gekks
@nene7292
@nene7292 Жыл бұрын
How come we can't do newHead.next = node instead of head.next.next = head? Doing this gives the wrong answer but logically it seems to be the same thing to me..=(
@BenCreasyAlaska
@BenCreasyAlaska 3 жыл бұрын
if not head: return None This condition never executes - the if head.next condition which comes later ensures that head reverseList is never passed a None head However I see that leetcode won't pass it without this condition. I guess that's one of those validation cases where the function is called with None directly?
@xenomagpie4484
@xenomagpie4484 2 жыл бұрын
cause the list could be empty
@rikki146
@rikki146 2 жыл бұрын
@@xenomagpie4484 my man
@lembueno894
@lembueno894 8 ай бұрын
i got it by myself (39ms py3), but my god i had to rewire my brain for 30 minutes by talking myself through it
@khaleelfreeman
@khaleelfreeman 7 ай бұрын
Spent some time refactoring the answer to make it more "classical" recursion. Hope it helps other people. fun reverseList(head: ListNode?): ListNode? { return if (head == null) { null } else if (head.next == null) { head } else { val newHead = reverseList(head.next) head.next?.next = head head.next = null newHead } }
@neilmurphy7594
@neilmurphy7594 26 күн бұрын
@NeetCode What are you using for a tablet/whiteboarding software?
@victorwu3715
@victorwu3715 5 ай бұрын
why do we have to create another reference to the head node using current? Can't we just use head?
@sarmadsohaib3032
@sarmadsohaib3032 Жыл бұрын
Hi, can you please elaborate why you did cure.next = prev? Thank you very much
@sudeepkuchara5287
@sudeepkuchara5287 Жыл бұрын
curr.next should point back so prev holds the pointer to previous pointer
@jxswxnth
@jxswxnth Жыл бұрын
try to make a visual representation once on paper it helps a lot.
@awatcherman
@awatcherman 2 ай бұрын
// using recursion in c# public ListNode ReverseList(ListNode? curr, ListNode? prev = null) { if (curr == null) return prev; var tmp = curr.next; curr.next = prev; return ReverseList(tmp, curr); }
@ValentinSharonov
@ValentinSharonov 6 ай бұрын
Can someone explain why in recursion solution newHead is changing when head is changing?
@rogerthat7190
@rogerthat7190 Жыл бұрын
why do we return prev in the iterative solution, are we normally just supposed to return the head?
@jointcc2
@jointcc2 Жыл бұрын
because the two pointers march through the nodes such that prev starts with None and head starts with the first node, so when the loop finishes, prev points to the last node and head points to None, so we definitely need to return prev as the new head (in the last iteration prev gets updated to the last node and the head gets updated as None)
@spencerfitzgerald908
@spencerfitzgerald908 4 ай бұрын
The confusing part with his recursive solution is that the base case he calls out is incorrect. The base case he has is actually the edge case when no node is passed into the function. Try throwing in a print statement and you'll see that it's only run when the input is None: if not head: print('This is the edge case when the input is None') return None Really, the base case is when "head.next == None" (in Python speak "if not head.next"). I rewrote the recursive solution with the base case more apparent: def reverseListRecursive(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: print('This is the edge case when the input is None') return None if not head.next: print('This is the base case. At this point, we are at the last node in the list.') newHead = head else: newHead = self.reverseListRecursive(head.next) head.next.next = head head.next = None return newHead
@rodo2220
@rodo2220 Ай бұрын
Why can't I just reverse the list using slicing? I'm still pretty new to coding, but it seems hella easier to just type out head[::-1] and call it a day. I'm guessing it has something to do with the time complexity?
@gsparmy6299
@gsparmy6299 3 ай бұрын
all i can say is thank uuuuuuuu
@gurunathreddy6908
@gurunathreddy6908 Жыл бұрын
i felt confused why we need to store in a temporary variable nxt=curr.next
@h0td0g
@h0td0g 9 ай бұрын
the hardest thing to grasp here is how you return a new head while manipulating the existing linked list. its giving me a headache. this is one of those solution where i would say recursion is not the ideal method for it.
@Abubakar91718
@Abubakar91718 10 ай бұрын
I completed this without any help in just 4 hours😅
@suketu1
@suketu1 Ай бұрын
iterative solution more intuitive then recursive for this problem
@martinemanuel8239
@martinemanuel8239 Жыл бұрын
Stucked with recursive way? See this example: a -> b "head.next" is (b) right? So I want my "b.next" pointing to previous! a
@funkaadda3207
@funkaadda3207 Жыл бұрын
Hi,This is rohini.I was tried the below code for reversing a linked list. but it shows error like non-type object has no attribute ' next'. I didn't get what the error is dummy=ListNode() dummy1=dummy while head: tail=head while tail.next.next: tail=tail.next dummy.next=tail dummy=dummy.next tail.next=None return dummy1.next
@XxM1G3xX
@XxM1G3xX 4 ай бұрын
class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: return self.recursive(None, head) def recursive(self, prev: ListNode, curr: ListNode): if not curr: return prev newNext = curr.next curr.next = prev return self.recursive(curr, newNext)
@m____s
@m____s Ай бұрын
def reverseList(head): if (head is None) or (head.next is None): return head newhead = reverseList(head.next) head.next.next = head head.next = None return newhead
@stephentrewick1383
@stephentrewick1383 25 күн бұрын
nxt is the 3rd pointer. So its a three pointer problem, isn't it???
@YashArya01
@YashArya01 3 жыл бұрын
On Line 17, shouldn't this be inside the if condition? outside the if, head.next is null already.
@NeetCode
@NeetCode 3 жыл бұрын
Good point! somehow i missed that, but luckily the code works whether we put it inside or leave it outside the if-condition.
@ashleywilkonson386
@ashleywilkonson386 3 жыл бұрын
@@NeetCode you could also check if head or head.next is None in the base case and return the head if it is since you'd be at the input list's tail which would be the new head. After that, the function would just be the four lines under the if statement.
@abdul.arif2000
@abdul.arif2000 11 ай бұрын
class Solution: def reverseList(self, head: ListNode) -> ListNode: # Recursive answer # Base case: If the list is empty or has only one node, it's already reversed. if not head or not head.next: return head # Recursive call to reverse the remaining part of the linked list. Break it down to sub problems to keep reversing. # reversed_head will be the head of the reversed linked list for the rest of the nodes. reversed_head = self.reverseList(head.next) # At this point, head.next points to the next node in the original list, which will be the last node # in the reversed linked list. We need to make the next node point to the current head node, effectively reversing the link. print(head.next.next) head.next.next = head print(head.next.next.val) # Set the current head's next pointer to None, as it will be the last node in the reversed linked list. head.next = None # Return the new head of the reversed linked list (reversed_head). return reversed_head
@MeowMaster176
@MeowMaster176 10 ай бұрын
For your recursive solution, why is the memory complexity O(n)?
@rushabhmehta6721
@rushabhmehta6721 9 ай бұрын
Recursion call stack takes up memory
@s4ltokyo
@s4ltokyo 2 жыл бұрын
For those confused about head.next.next. Basically just think of it as pointing from the next node to pointing to itself.
@kevinchen1570
@kevinchen1570 Жыл бұрын
Also, for those who are confused about the code line head.next.next = head. Here’s one explanation to hopefully clarify one line of confusion since I was confused for the longest time :P. So assignment lines of code such as head.next.next = head are executed right to left (get the value of head first and then assign it to head.next.next). Thus in the case after returning from the base case, the stack frame’s head function parameter value at that moment is referring to Node 2. Therefore if head=NODE 2, then (head.next) refers the NODE 3, and then (head.next).next or (NODE 3).next refers to the POINTER of node 3. Be careful NOT to focus too much on the Null/None value of the pointer since that’s where my confusion was. I was thinking head.next.next meant if head = Node2, then head.next=Node3 and then head.next.next = null, so then head.next.next = head translates to null = Node 2. And that’s where my mistake and confusion came in. Long story short, the role of head.next.next is to reverse the order of the nodes and instead of thinking that head.next.next refers to null, think that head.next.next refers to the POINTER of Node3. As a result, head.next.next = head, reassigns the Node2 (head) to Node3's next pointer (head.next.next) or in other words Node3s next pointer is now assigned to point at Node2 which reverses the order.
@wschoi98
@wschoi98 9 ай бұрын
@@kevinchen1570 great!!!!
@hipsterloopy
@hipsterloopy Жыл бұрын
why do we need a curr pointer. cant we just use head as is?
@bhosaleraje1
@bhosaleraje1 Жыл бұрын
can anyone explains head.next.next step?
@mihail263
@mihail263 2 жыл бұрын
If I have (head = 1234), then won't doing (head.next.next= head) make (head = 121234) ???
@mihail263
@mihail263 Күн бұрын
It creates a cycle 1-2-1-2-1....
@LC-bc2yv
@LC-bc2yv Жыл бұрын
That head.next.next is confusing AF.
@alexandercannon3329
@alexandercannon3329 2 жыл бұрын
Great explanation! Quick question - why do we return "prev" at the end rather than just the "curr" node if the "curr" node should be reversed?
@minyoungan9515
@minyoungan9515 2 жыл бұрын
@Alexander In the iterative solution, "curr" will be null/none at the end, and "prev" will be the new head node/original tail node.
@list9016
@list9016 2 жыл бұрын
At the end, current will be None because when we're reversing we go to the next element in each iteration and in a case like this: 1 -> 2 -> 3 -> 4 -> None Once it goes to 4 and reverses it, in the same iteration it'll set curr to None. BUT None's previous node is 4 which we can use as the node.
@tanmaysawant324
@tanmaysawant324 2 жыл бұрын
@Alexander. As you can see we are passing "head.next" recursively to recreverseList(self, head). According to our condition, if head == None or head.next == None return head. That is after passing "head.next" recursively we get, if "head.next.next == None" return head, which is our second last node. Then we proceed further for reversing the list.
@khoantum
@khoantum 2 жыл бұрын
head.next.next = head explanation: linkedlist of 1 => 2 => 3 base case focuses on 2 => 3 where 2 is the head Head.next.next = head means 3.next = 2 (the head) so this makes a 2 => 3 circular linked list (which means 2 => 3 & 2 3(remember we still have 2 3. Now the result is 2 2 and repeat.
@amandafinbarrs2060
@amandafinbarrs2060 2 жыл бұрын
Loved this explanation. I didn't completely understand the video. Thank you so much!
@user-hy4sz8lx5z
@user-hy4sz8lx5z 2 жыл бұрын
Thank you so much, I was very confused because of that. Your explanation was amazing and easy to understand.
@gorsama-2190
@gorsama-2190 Жыл бұрын
OMG Ty very much
@phuongnguyen-ih3yq
@phuongnguyen-ih3yq Жыл бұрын
Thank you so much! Now i understand
@sriharika2775
@sriharika2775 Жыл бұрын
Thank you so much...
@karavind7814
@karavind7814 Жыл бұрын
Here's how head.next.next = head works From the video we want the lastNode/tail of the reversed node to point of the current node. head.next is tailNode (of reversedList) head.next.next is tailNode.next head.next.next = head is taiilNode.next = head. ---------------------------------------------------- Easier Version of Code: ``` def reverseList(node) : if not node: return newHead = head if head.next: newHead = reverseList(head.next) tailNode = head.next tailNode.next = head head.next = None return newHead; ```
@astalma
@astalma Жыл бұрын
Maybe this solution will be more readable for somebody: public ListNode ReverseListRecursive(ListNode head) { if (head == null) // edge case - no elements in our list return null; if (head.next == null) // we are at the end of our linked list return head; // so we just return current head var newHead = ReverseListRecursive(head.next); // we reverse sublist starting from next element head.next.next = head; // we change link to next element for next element so it looks at current head head.next = null; // now our current head is at the end for new reversed list so next must be null return newHead; }
@PROTAGONIST_48
@PROTAGONIST_48 2 жыл бұрын
Better recursive solution in C++ Node* reverseList(Node* head, Node* previousNode = nullptr){ if(head==nullptr){ return previousNode; } Node* newHead {reverseList(head->next, head)}; head->next = previousNode; return newHead; }
@Anonymousbirb19
@Anonymousbirb19 Жыл бұрын
That recursive solution was extremely confusing
@PhanNghia-fk5rv
@PhanNghia-fk5rv Ай бұрын
i understand your recursion approach but i think it's way complicate due to head.next = None just effectively need to use 1 time Other approach in coment much reachable
@PippyPappyPatterson
@PippyPappyPatterson 2 жыл бұрын
Is this the complexity? O(n) time O(n) space
@saugatgarwa6389
@saugatgarwa6389 2 жыл бұрын
It is done in O(N) time O(1) space
@PippyPappyPatterson
@PippyPappyPatterson 2 жыл бұрын
@@saugatgarwa6389 I don't think so. There is a recursive call for each element `n`, hence `n` function calls on the stack. Please correct me if I'm wrong.
@saugatgarwa6389
@saugatgarwa6389 2 жыл бұрын
@@PippyPappyPatterson Yes, you're correct. I mentioned the time and space complexity for the Iterative method cuz that seemed the most understandable and efficient to me :D
@PippyPappyPatterson
@PippyPappyPatterson 2 жыл бұрын
@@saugatgarwa6389 Ahh, that makes sense. Agreed on the iterative implementation!
@flatmapper
@flatmapper 4 ай бұрын
3 pointers solution not 2)
@jasonxb2998
@jasonxb2998 Жыл бұрын
POINT OF CONFUSION 1: Leetcode gives an example where it represents a linked list in the form of an array (misleading) - Your function is not supposed to accept arrays - It's supposed to accept a linked list as objects linked together with next properties You can construct a linked list using the constructor function they provide commented out in their built in code editor They generate linked lists behind the scenes to test your function out POINT OF CONFUSION 2: The head parameter in your function is just the linked list in its entirety The list begins at a certain object, so that's they they refer to it as the head
@hwang1607
@hwang1607 11 ай бұрын
Can someone explain the recursive code
@xudong5269
@xudong5269 3 ай бұрын
still not understanding the recursive method
@adityahpatel
@adityahpatel 3 ай бұрын
It is not clear at all - what is the head? without roting/memorizing, can u tell what is the type of input head? No, it is not a listnode. because if u do print(head.data) it will throw an error....so what is it?
@LonliLokli
@LonliLokli Жыл бұрын
I do not like this question as all the solutions are in-place, ie modifying input, while it was not mentioned in description and not obvious
@shleebeez
@shleebeez Жыл бұрын
Does anyone know why the input and output on leetcode looks like arrays now and doesn't point to NULL anymore?
@ndruhrkv
@ndruhrkv Жыл бұрын
One more solution (js) We go through list to the end and do all job at bubbling phase var reverseList = function(head) { function reverse(prev, current) { if (!current) { return prev; } const head = reverse(current, current.next); current.next = prev; return head; } return reverse(null, head); };
@nero9985
@nero9985 2 жыл бұрын
Recursion makes me wanna puke
@alexandroskourtis5268
@alexandroskourtis5268 2 жыл бұрын
@@irispallis brain gave me 500 error code
@anjanov
@anjanov 2 жыл бұрын
@@alexandroskourtis5268 def study_recursion(puke): if puke == 0: print('Congrats, you are done with recursion') return puke -= 1 study_recursion(puke)
@sangpark7656
@sangpark7656 7 ай бұрын
what's the purpose of next here?
@failure795
@failure795 Жыл бұрын
ADS
@yizhang7027
@yizhang7027 Жыл бұрын
Just create a new list, then add each node of the old list, one by one from head to tail, to the front of the new list.
@adityaprabhu6
@adityaprabhu6 9 ай бұрын
Shouldn't the recursive solution begin with "while head.next" instead of "if head.next" - otherwise the branch executes once?
@shrijeet.gaikwad
@shrijeet.gaikwad 8 ай бұрын
No. Otherwise it's iterative problem all over again. Also that is base case
@tsunningwah3471
@tsunningwah3471 4 ай бұрын
kjvgkjbjkbkjb1
@sabinbajracharya3815
@sabinbajracharya3815 2 жыл бұрын
Kotlin recursive solution fun reverseList(head: ListNode?): ListNode? { if (head?.next == null) { return head } val root = reverseList(head.next) val nextNode = head.next nextNode.next = head head.next = null return root }
@sabinbajracharya3815
@sabinbajracharya3815 2 жыл бұрын
The "root" is the root node of the new reversed linkedList. So there are two task of reverseList method 1. Propagate the "root" Node in the recursive chain 2. Fix the node pointing at each other. - i.e The head is the "previous node" and we get the next node from head. Then the nextNode will point back to head (i.e previous node) and finally remove the node that is pointed by the head (i.e. previous node). Basically we reverse the arrow between the previous (head) and the next node.
@thespeedyyoshi
@thespeedyyoshi 7 ай бұрын
easy....hah
@meerasarna6862
@meerasarna6862 Жыл бұрын
Why did you mention we only need two pointers. We need three pointers. At least explain in correct way.
@mondaymorningassemblynewsl5496
@mondaymorningassemblynewsl5496 4 ай бұрын
tbh this was not a helpful video
@yuangao3117
@yuangao3117 6 ай бұрын
Hello, just a very naive question: why 'head.next.next=head' instead of 'newHead.next=head', thanks!
@spencerfitzgerald908
@spencerfitzgerald908 4 ай бұрын
'newHead' is meant to be the last node in the linked list. 'newHead' does not equal the 'next' node. The function keeps returning 'newHead' from each recursive call until it's returned to the original caller.
@ninjaturtle205
@ninjaturtle205 Жыл бұрын
def reverse(node): if node.next is None: return node, node else: nextNode, head = reverse(node.next) nextNode.next = node node.next = None return node, head node, ReversedHead = reverse(LinkedList.head) LinkedList.head = ReversedHead
L9. Reverse a LinkedList | Iterative and Recursive
32:42
take U forward
Рет қаралды 81 М.
LeetCode - Reverse Linked List Solution
7:02
Nick White
Рет қаралды 121 М.
когда повзрослела // EVA mash
00:40
EVA mash
Рет қаралды 2,4 МЛН
Stupid Barry Find Mellstroy in Escape From Prison Challenge
00:29
Garri Creative
Рет қаралды 21 МЛН
10 Math Concepts for Programmers
9:32
Fireship
Рет қаралды 1,8 МЛН
Reverse Linked List - Leetcode 206 - Linked Lists (Python)
8:02
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 203 М.
Reverse Linked List - LeetCode  206 - Python (Iterative and Recursive)
15:53
Group Anagrams - Categorize Strings by Count - Leetcode 49
8:12
💅🏻Айфон vs Андроид🤮
0:20
Бутылочка
Рет қаралды 739 М.
В России ускорили интернет в 1000 раз
0:18
Короче, новости
Рет қаралды 349 М.
cute mini iphone
0:34
승비니 Seungbini
Рет қаралды 5 МЛН
SSD с кулером и скоростью 1 ГБ/с
0:47
Rozetked
Рет қаралды 565 М.