LeetCode Reorder List Solution Explained - Java

  Рет қаралды 32,343

Nick White

Nick White

Күн бұрын

Пікірлер
@yachnagupta2081
@yachnagupta2081 5 жыл бұрын
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.
@HarshaKalluri
@HarshaKalluri 5 жыл бұрын
thats extra space.
@safiabegum7060
@safiabegum7060 4 ай бұрын
Can u be more clear
@rifathossain2440
@rifathossain2440 4 жыл бұрын
My mans always looking high :)
@anshulsingh1265
@anshulsingh1265 3 жыл бұрын
Agree😂😂😂
@miadinh6381
@miadinh6381 2 жыл бұрын
Strategy starts from 3:09
@shamanthhegde2786
@shamanthhegde2786 4 жыл бұрын
can you say how the merging condition breaks in odd number of linked list.
@jollysrivastava3326
@jollysrivastava3326 4 жыл бұрын
Hi Nick, you may need to rework on your logic for the merge. It's not working fine for odd length array.
@ommule4999
@ommule4999 3 жыл бұрын
This logic works for odd & even length LinkedLists ! No issues
@jacksonripper-mp8dr
@jacksonripper-mp8dr 8 ай бұрын
and you need to rework on developing your brain.....
@jollysrivastava3326
@jollysrivastava3326 8 ай бұрын
@@jacksonripper-mp8dr I did. Thanks for letting me know. I just forgot to use my brain.
@jacksonripper-mp8dr
@jacksonripper-mp8dr 8 ай бұрын
@@jollysrivastava3326 I was just kidding.... Everyone learns from mistakes.
@aishwarygupta19
@aishwarygupta19 8 ай бұрын
Does anyone else getting a "Time Limit Exceeded" error
@samwilson4597
@samwilson4597 2 жыл бұрын
I first tried it with a stack. this solution is a bit tricky
@vikramreddy7586
@vikramreddy7586 4 жыл бұрын
My dogs are barking in the background :D Like always an awesome explanation. !!
@vasurohilla5812
@vasurohilla5812 10 ай бұрын
where can i found the source code
@nik15oc
@nik15oc 4 жыл бұрын
Whats the time and space complexities? Time Complexity: O(n) & Space Complexity: O(1) ?
@abe10
@abe10 4 жыл бұрын
Yeah, I guess. We traverse all the nodes twice and constant space for temporary variables.
@raginibhadani7257
@raginibhadani7257 Жыл бұрын
merge logic does not work correctly
@safiabegum7060
@safiabegum7060 4 ай бұрын
Explain merge function again Mergin both lists is difficult
@DheerajSharma-fs6je
@DheerajSharma-fs6je Жыл бұрын
Thank You!!
@akshitgupta7665
@akshitgupta7665 4 жыл бұрын
brilliant
@prathamanand1037
@prathamanand1037 2 жыл бұрын
thanks
@honey-xr5kp
@honey-xr5kp 7 ай бұрын
doesnt work for me
@antoniomartinez3429
@antoniomartinez3429 4 жыл бұрын
what about traversing the list and pushing values into an array first?
@rajarshibose5122
@rajarshibose5122 4 жыл бұрын
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 ...
@ianpan0102
@ianpan0102 4 жыл бұрын
@@rajarshibose5122 agreed
@Adarsh-mn7pl
@Adarsh-mn7pl 2 жыл бұрын
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]
@arishsheikh3000
@arishsheikh3000 2 жыл бұрын
Split it into 1,2,3 and 4,5
@jagdishwarbiradar1763
@jagdishwarbiradar1763 4 жыл бұрын
fast and slow doesn't work properly
@ramesh_hegde
@ramesh_hegde Ай бұрын
nice dog bark in background
@ianpan0102
@ianpan0102 4 жыл бұрын
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; } };
@abivilion8559
@abivilion8559 2 жыл бұрын
brilliant buddy
@viditsharma3929
@viditsharma3929 3 жыл бұрын
great.
@johncho9160
@johncho9160 10 ай бұрын
the way youre explaining the merge method is confusing af and the way youre naming these variables certainly dont help
@millenialmusings8451
@millenialmusings8451 2 жыл бұрын
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
@wirelessbrain12
@wirelessbrain12 2 жыл бұрын
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.
LeetCode 146. LRU Cache (Algorithm Explained)
18:00
Nick White
Рет қаралды 119 М.
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
HARD_MMA
Рет қаралды 3,3 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 2,1 МЛН
LeetCode Reverse Linked List II Solution Explained - Java
8:34
Nick White
Рет қаралды 36 М.
LeetCode Decode String Solution Explained - Java
10:52
Nick White
Рет қаралды 59 М.
LeetCode 442. Find All Duplicates in an Array (Solution Explained)
12:37
LeetCode 48. Rotate Image (Solution Explained)
10:18
Nick White
Рет қаралды 86 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 674 М.
Intro to Java Programming - Course for Absolute Beginners
3:48:25
freeCodeCamp.org
Рет қаралды 3,5 МЛН
LeetCode 238. Product of Array Except Self (Solution Explained)
14:49