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

  Рет қаралды 427,031

NeetCode

NeetCode

Күн бұрын

Пікірлер: 196
@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 2 жыл бұрын
@@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 2 жыл бұрын
Easiest recursive to understand.
@batnasn
@batnasn 2 жыл бұрын
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)
@manishmaharjann
@manishmaharjann 2 жыл бұрын
I have struggled so much on this simple problem. You explained it so simply. You are genius thanks for that !
@pablovaldes2397
@pablovaldes2397 27 күн бұрын
no one is a genius, some people just practice more, and a rare few people practice enough that they see new patterns other could not and therefore expand human knowledge with their gained intuition
@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 Жыл бұрын
Yea it's honestly making me feel like an idiot
@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 8 ай бұрын
@@Inconvenient-MRKim May I ask if this was a faang company?
@Inconvenient-MRKim
@Inconvenient-MRKim 8 ай бұрын
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
@h3ckphy246
@h3ckphy246 Ай бұрын
Hey, what's up? Did you get to faang?
@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
@AnthonyInSanDiego
@AnthonyInSanDiego 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 2 жыл бұрын
Thanks, this comment is worth pining
@jxswxnth
@jxswxnth 2 жыл бұрын
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 11 ай бұрын
i lost hope in understanding the recursive solution and then saw your comment. thank you Josh,
@KSMuralidhar
@KSMuralidhar 11 ай бұрын
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; } -------------------------------------------------------------------------
@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!
@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.
@Saotsu1
@Saotsu1 Ай бұрын
My simple recursive solution: class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: def r(prev, node): if not node: return prev first = r(node, node.next) node.next = prev return first return r(None, head)
@JimmyCheng
@JimmyCheng 2 жыл бұрын
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 7 ай бұрын
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)
@sujalkumarsinha
@sujalkumarsinha 10 ай бұрын
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 .
@eshaanmehta9537
@eshaanmehta9537 11 ай бұрын
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!
@the-tankeur1982
@the-tankeur1982 Жыл бұрын
Every time i'm amazed by how much you can simplify solutions and how well you can explain thank you for all that
@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 2 жыл бұрын
OMG Ty very much
@phuongnguyen-ih3yq
@phuongnguyen-ih3yq 2 жыл бұрын
Thank you so much! Now i understand
@sriharika2775
@sriharika2775 Жыл бұрын
Thank you so much...
@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 5 ай бұрын
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.
@bruce716
@bruce716 2 жыл бұрын
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 Жыл бұрын
bro your explanation helped a lot thanks man
@shatakshivishnoi916
@shatakshivishnoi916 2 жыл бұрын
your tutorials help a lot..thankyou!!
@shreerambhat1442
@shreerambhat1442 5 ай бұрын
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
@spencerfitzgerald908
@spencerfitzgerald908 7 ай бұрын
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
@rajenderkatkuri7642
@rajenderkatkuri7642 2 жыл бұрын
You make it seem effortless. ❤️
@100bands
@100bands Жыл бұрын
Thanks!
@khaleelfreeman
@khaleelfreeman 9 ай бұрын
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 } }
@yevhendubinin
@yevhendubinin 9 ай бұрын
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 }
@CT--TranAnhTuan
@CT--TranAnhTuan 12 сағат бұрын
if (head == null){ return head; } // in- place reversal of linked list ListNode prev = null; ListNode curr = head; while (curr!=null){ prev = new ListNode(curr.val, prev); curr = curr.next; } return prev; is this logically right?
@jamesdouthit7489
@jamesdouthit7489 5 ай бұрын
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)
@bulioh
@bulioh 2 жыл бұрын
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
@martinemanuel8239
@martinemanuel8239 2 жыл бұрын
Stucked with recursive way? See this example: a -> b "head.next" is (b) right? So I want my "b.next" pointing to previous! a
@m____s
@m____s 4 ай бұрын
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
@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
@AustinCS
@AustinCS Ай бұрын
I think your recursive solution here sacrifices significant readability, but it works lol. I don't understand how your brain works like that lol: class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: def reverse(prev, curr): if curr is None: return prev head = reverse(curr, curr.next) curr.next = prev return head return reverse(None, head)
@2NormalHuman
@2NormalHuman 2 жыл бұрын
Really good explanation! Thank you
@navrajsingh8829
@navrajsingh8829 20 күн бұрын
Simple!!! # Recursive def reverseList(self, head): if not head or not head.next: return head nextNode = head.next reverseHead = self.reverseList(nextNode) nextNode.next = head head.next = None return reverseHead # Iterative def reverseList(self, head): prev = None while head: temp = head.next head.next = prev prev = head head = temp return prev
@pinakadhara7650
@pinakadhara7650 Жыл бұрын
Awesome explanation
@NeetCode
@NeetCode Жыл бұрын
Glad it was helpful!
@user-fk1wo2ys3b
@user-fk1wo2ys3b Жыл бұрын
HUGE THANKS!
@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
@sanooosai
@sanooosai Жыл бұрын
thank you sir
@awatcherman
@awatcherman 5 ай бұрын
// 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); }
@XxM1G3xX
@XxM1G3xX 7 ай бұрын
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)
@gsparmy6299
@gsparmy6299 6 ай бұрын
all i can say is thank uuuuuuuu
@thebiglongshlong
@thebiglongshlong 2 жыл бұрын
goat talk!
@jasonxb2998
@jasonxb2998 2 жыл бұрын
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
@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.
@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..=(
@kpdkchristian2570
@kpdkchristian2570 10 күн бұрын
spent 20 minutes trying to figure out how to do this with 2 pointers as advertised @1:10 only to find that you actually use a 3rd in the implementation lol
@sarmadsohaib3032
@sarmadsohaib3032 2 жыл бұрын
Hi, can you please elaborate why you did cure.next = prev? Thank you very much
@sudeepkuchara5287
@sudeepkuchara5287 2 жыл бұрын
curr.next should point back so prev holds the pointer to previous pointer
@jxswxnth
@jxswxnth 2 жыл бұрын
try to make a visual representation once on paper it helps a lot.
@alexandersmirnov4274
@alexandersmirnov4274 7 ай бұрын
that was helpful
@osmanmusse9432
@osmanmusse9432 2 жыл бұрын
Great Explanation
@Mamtagoyal27
@Mamtagoyal27 2 жыл бұрын
@NeetCode, Thanks for putting this video but I think Line #13: newHead = Head is not required at all.
@neilmurphy7594
@neilmurphy7594 3 ай бұрын
@NeetCode What are you using for a tablet/whiteboarding software?
@mrkandreev
@mrkandreev 6 ай бұрын
It is much more easier to use one line assignment: curr.next, prev, curr = prev, curr, curr.next
@lembueno894
@lembueno894 10 ай бұрын
i got it by myself (39ms py3), but my god i had to rewire my brain for 30 minutes by talking myself through it
@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 8 ай бұрын
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)
@whiletrue1-wb6xf
@whiletrue1-wb6xf 9 күн бұрын
How while curr can run if its equal to None ?
@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; }
@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 2 жыл бұрын
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 11 ай бұрын
@@kevinchen1570 great!!!!
@victorwu3715
@victorwu3715 7 ай бұрын
why do we have to create another reference to the head node using current? Can't we just use head?
@suketu1
@suketu1 3 ай бұрын
iterative solution more intuitive then recursive for this problem
@ValentinSharonov
@ValentinSharonov 8 ай бұрын
Can someone explain why in recursion solution newHead is changing when head is changing?
@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; }
@andrewvirtual
@andrewvirtual Жыл бұрын
Explained it much better than Geeks for Gekks
@h0td0g
@h0td0g 11 ай бұрын
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.
@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)
@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
@janedisha
@janedisha 2 ай бұрын
Can anyone please tell me, how to code this program on a locally installed version of python. I mean, in neet/leet code, linked lists are predefined. I can't solve this problem on my local pc, without defining a list first.
@vivekbinjola2460
@vivekbinjola2460 2 жыл бұрын
helpful
@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); };
@PhanNghia-fk5rv
@PhanNghia-fk5rv 3 ай бұрын
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
@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.
@gurunathreddy6908
@gurunathreddy6908 Жыл бұрын
i felt confused why we need to store in a temporary variable nxt=curr.next
@abdul.arif2000
@abdul.arif2000 Жыл бұрын
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
@rodo2220
@rodo2220 3 ай бұрын
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?
@ryant3204
@ryant3204 2 ай бұрын
Go and find out what is a linked list first
@ryant3204
@ryant3204 2 ай бұрын
A linked list is not the same as a "list" in python, which is actually a dynamic array
@mihail263
@mihail263 2 жыл бұрын
If I have (head = 1234), then won't doing (head.next.next= head) make (head = 121234) ???
@mihail263
@mihail263 2 ай бұрын
It creates a cycle 1-2-1-2-1....
@xudong5269
@xudong5269 5 ай бұрын
still not understanding the recursive method
@MeowMaster176
@MeowMaster176 Жыл бұрын
For your recursive solution, why is the memory complexity O(n)?
@rushabhmehta6721
@rushabhmehta6721 Жыл бұрын
Recursion call stack takes up memory
@stephentrewick1383
@stephentrewick1383 3 ай бұрын
nxt is the 3rd pointer. So its a three pointer problem, isn't it???
@dibyendusaha1295
@dibyendusaha1295 Ай бұрын
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode ReverseList(ListNode head) { ListNode node1=null; while(head!=null) { node1=new ListNode(head.val,node1); head=head.next; } return node1; } }
@Abubakar91718
@Abubakar91718 Жыл бұрын
I completed this without any help in just 4 hours😅
@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; ```
@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!
@prikshitbatta
@prikshitbatta Жыл бұрын
why are you returning prev in two pointer approach?
@arjundureja
@arjundureja Ай бұрын
because cur will be null at the end of the loop
@bhosaleraje1
@bhosaleraje1 2 жыл бұрын
can anyone explains head.next.next step?
@hipsterloopy
@hipsterloopy Жыл бұрын
why do we need a curr pointer. cant we just use head as is?
@adityahpatel
@adityahpatel 5 ай бұрын
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?
@Sruthi9
@Sruthi9 Ай бұрын
watch in 1.5x
@flatmapper
@flatmapper 6 ай бұрын
3 pointers solution not 2)
@LC-bc2yv
@LC-bc2yv 2 жыл бұрын
That head.next.next is confusing AF.
@hwang1607
@hwang1607 Жыл бұрын
Can someone explain the recursive code
@Anonymousbirb19
@Anonymousbirb19 Жыл бұрын
That recursive solution was extremely confusing
@brhiandavila6987
@brhiandavila6987 Ай бұрын
The recursive solution was so unbelievably hard to understand, I tried to draw it out and everything. I could not, just look at the comments, its a much more efficient approach for understanding and saving time. No offense. The iterative solution was good though!
@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
@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.
@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.
@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)
@treya111
@treya111 2 ай бұрын
the ad is so long😢
@sangpark7656
@sangpark7656 9 ай бұрын
what's the purpose of next here?
@failure795
@failure795 Жыл бұрын
ADS
@adityaprabhu6
@adityaprabhu6 Жыл бұрын
Shouldn't the recursive solution begin with "while head.next" instead of "if head.next" - otherwise the branch executes once?
@shrijeet.gaikwad
@shrijeet.gaikwad 10 ай бұрын
No. Otherwise it's iterative problem all over again. Also that is base case
@meerasarna6862
@meerasarna6862 Жыл бұрын
Why did you mention we only need two pointers. We need three pointers. At least explain in correct way.
@tsunningwah3471
@tsunningwah3471 6 ай бұрын
kjvgkjbjkbkjb1
L9. Reverse a LinkedList | Iterative and Recursive
32:42
take U forward
Рет қаралды 127 М.
Reverse Linked List II - Leetcode 92 - Python
16:03
NeetCode
Рет қаралды 78 М.
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 6 МЛН
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 119 МЛН
Throwing Swords From My Blue Cybertruck
00:32
Mini Katana
Рет қаралды 11 МЛН
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 303 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 230 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 491 М.
Reverse Linked List - Leetcode 206 - Linked Lists (Python)
8:02
Software Engineering Job Interview - Full Mock Interview
1:14:29
freeCodeCamp.org
Рет қаралды 1,4 МЛН
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Maximum Twin Sum of a Linked List - Leetcode 2130 - Python
8:59
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 348 М.
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 6 МЛН