LeetCode Reversed LinkedList Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Just to simply further:
Result of 1st iteration of 4 lines of code in tha while loop:
// next = 2
// 1-> null
//prev = 1
// head =2

2nd:
//now, next = 3
// 2->1 (remember 1 already points to null)
// prev = 2
// head = 3

WhatIsThisAllAbout
Автор

I think you go too fast, imagine someone who is trying to learn this from scratch, they would get confused. But for a quick review before an interview or an exam, this is perfect. Keep it up man.

kelvincheng
Автор

For the viewers trying to get pass through interviews: Also consider using a Stack. It is the same in space and equals in time complexities but you introduce another data structure, which is technicly worse space, not theoreticly. Start by explaining the stack and when the interviewer asks for a better solution come up with this ;)

FianoGostaDeQueijo
Автор

Given the list:

1 -> 2 -> 3 -> 4 -> null

Initial state:

prev = null
head = 1
Iteration 1:

next = 2
Reverse: 1 -> null
Move pointers: prev = 1, head = 2
Iteration 2:

next = 3
Reverse: 2 -> 1 -> null
Move pointers: prev = 2, head = 3
Iteration 3:

next = 4
Reverse: 3 -> 2 -> 1 -> null
Move pointers: prev = 3, head = 4
Iteration 4:

next = null
Reverse: 4 -> 3 -> 2 -> 1 -> null
Move pointers: prev = 4, head = null
Final state:

prev now points to 4, which is the head of the reversed list: 4 -> 3 -> 2 -> 1 -> null
Complexity:
Time Complexity: O(n), where n is the number of nodes in the linked list. Each node is visited once.
Space Complexity: O(1), since the reversal is done in place without requiring additional space proportional to the size of the list.
This is an efficient and standard approach to reversing a linked list iteratively

maddy
Автор

every youtuber needs their signature introduction and I think the little clap is hilarious. I love the speed of explanation. perfect videos. thanks nick

austinredenbaugh
Автор

If anyone is interested in recursive version of it:
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* answer = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return answer;
}
};

NeerajSharma-ozmm
Автор

class Solution(object):
def reverseList(self, head):
prev = None
while head:
curr = head
head = head.next
curr.next = prev
prev = curr
return prev

xhulianobrace
Автор

not great.. nice way to disparage your viewers when you say "if you don't understand this you need to go back and study ... " or whatever you said.

davidverzwyvelt
Автор

you could have also mentioned that..
its just like taking each element and inserting in the beginning of the new head..
in your example it is prev.

epicsagetriks
Автор

Nick White is the goat helping me a lot

nicolastanure
Автор

I wrote this on paper and then it was easy to understand your code. Thanks bro.

anmethings
Автор

I know this is an old video, but thank you a lot for your explanation it was very helpful! I hope all is well

enryost
Автор

Nice Video Nick...All the best for Your interview....

kanaparthikiran
Автор

Nice video, very clean, took me like 5 minuts wrinting in paper to understand ! thx men, you earn my like and suscribe

ulisesgtzr
Автор

Thanks bruv appreciated ur effort a lot

cloud
Автор

Watching this, , it's pretty clear you have just memorized those 4 lines and once you write it all down, then you can make sense of it.

untitled
Автор

why you return prev in the end .i think prev become the head of the new list is this true ??

mosbahhoucemeddine
Автор

Can you please use the java keyword "this. " As it makes it easier to follow object changes for me as im newer to programming.

chris_kouts