LeetCode Reorder List Solution Explained - Java
ฝัง
- เผยแพร่เมื่อ 23 ธ.ค. 2024
- The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
AlgoCademy - algocademy.com...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/...
Follow Me on X/Twitter - x.com/nickwhit...
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nick....
Become A Member - / @nickwhite
#coding #programming #softwareengineering
Strategy starts from 3:09
How about traversing LinkedList till middle using slow pointer (jumps one) and fast pointer(jumps two)....Then from middle put the nodes in the stack... Start traversing LinkedList from head keep poping elements from stack and insert at a gap of one node in LinkedList.
thats extra space.
Can u be more clear
My mans always looking high :)
Agree😂😂😂
can you say how the merging condition breaks in odd number of linked list.
Hi Nick, you may need to rework on your logic for the merge. It's not working fine for odd length array.
This logic works for odd & even length LinkedLists ! No issues
and you need to rework on developing your brain.....
@@jacksonripper-mp8dr I did. Thanks for letting me know. I just forgot to use my brain.
@@jollysrivastava3326 I was just kidding.... Everyone learns from mistakes.
where can i found the source code
Whats the time and space complexities?
Time Complexity: O(n) & Space Complexity: O(1) ?
Yeah, I guess. We traverse all the nodes twice and constant space for temporary variables.
My dogs are barking in the background :D Like always an awesome explanation. !!
what about traversing the list and pushing values into an array first?
That is something you are not allowed to do for most interviews.Else you can solve every problem like that.....Do that only when you have no other way possible ...
@@rajarshibose5122 agreed
I first tried it with a stack. this solution is a bit tricky
brilliant
Thank You!!
merge logic does not work correctly
doesnt work for me
Does anyone else getting a "Time Limit Exceeded" error
thanks
Explain merge function again
Mergin both lists is difficult
My C++ solution using the same idea:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
class Solution {
public:
void reorderList(ListNode* head) {
if (!head || !head->next || !head->next->next) return;
ListNode *slow = head, *fast = head, *head1 = head, *head2, *tmp1, *tmp2;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
head2 = slow->next;
slow->next = nullptr; // split list in half
head2 = reverseList(head2); // reverse 2nd half
while (head1 && head2) {
tmp1 = head1->next;
head1->next = head2;
tmp2 = head2->next;
head2->next = tmp1;
head1 = tmp1;
head2 = tmp2;
}
}
private:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
ListNode* tmp;
while (curr) {
tmp = curr->next;
curr->next = prev;
prev = curr;
curr = tmp;
}
return prev;
}
};
brilliant buddy
fast and slow doesn't work properly
nice dog bark in background
great.
Not working for this case
Input
[1,2,3,4,5]
stdout
1 2
5 4 3
Output
[1,5,2,4]
Expected
[1,5,2,4,3]
Split it into 1,2,3 and 4,5
the way youre explaining the merge method is confusing af and the way youre naming these variables certainly dont help
man you spend a lot of time on the intro talking unnecessary trivia.. the actual video starts at 3:09.. plz get straight to the point
No you just don't know what he's talking about before that. He's going through what your mindset should be when breaking down a problem. For ex he talks about 2 pointers because that's the solution for arrays and hence you would think of that to see if it's applicable but then explains why it's not.