Rotate List - Linked List - Leetcode 61 - Python
Vložit
- čas přidán 23. 07. 2024
- 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
Coding Solutions: • Coding Interview Solut...
Dynamic Programming Playlist: • House Robber - Leetco...
Tree Playlist: • Invert Binary Tree - D...
Linked List Playlist: • Reverse Linked List - ...
Problem Link: leetcode.com/problems/rotate-...
0:00 - Read the problem
1:40 - Drawing Explanation
6:32 - Coding Explanation
leetcode 61
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#rotate #list #python - Věda a technologie
6:49 "k modded -"
Rude interruption there
Clear explanation with clean code :)
Step 1: Compute length of list, storing a reference to tail node
Step 2: Find pivot node, which becomes new tail node
Step 3: Rotate list by reconnecting both parts
Best channel on Internet for DS and Algo 🎉 Thanks NeetCode ❤
I love you man, thanks for getting me a job!
Great video, keep up the incredible work! :)
very clear explanation, thanks as usual :)
Best teacher ever! Thank you :)
I always watch your videos, when I cannot solve algorithms by myself. You are good at explaining. Thank you bro.
Great drawing! Thanks!
great explanation! could you make a video on leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ?
great explanation bro and thanks alot
hello guys sorry i'm a beginner, when we assign the cur = head is it create a copy of the linked list or referencing the same linked list?
NeetCode is GREAT!!!
When I solve coding problems and use itereation, somehow, I think of iteration of all elements I think it's because many problems need to iterate. I think this is really important to break what I have known and think about the solution fit the best to the problem I currently encounter. My first approach was making a list then iterate them. Time: O(n) Space: O(n). After your explanation, I only replaced with two points. and I thought, okay it looks nice. Then I saw your solution. It iterates one more though, you don't use any space I mean O(1). Hoo, great. I really learned a lot from your videos. Your eyes to see problems is absolutely stand out
I wrote a recursive solution, it went good until the cases where the k number was huge appeared
Hey why dont you use 2-pointers method from Leetcode 19?
clean & neet
I love your way of explaining .
I knoooow, right? Nobody else can do it like he does, I absolutely love the way he explains things
U a God
Why we are doing k=k% length
i didnt understand the tail.next last line part after nhead become 45 how tail = head(12345) it should be added to the nhead , i am beginner pls help me
we already broke the connection between 123 and 45 when we set cur.next=None after exiting the loop therefore, head would now be (1->2->3). Now we essentially put this head at the end of the tail (which is 4->5). This gives us the desired rotated list which is 4->5->1->2->3
And what exactly happening in for loop
New head = cur.next ie 4
Cur.next = none means 3 -> none
What is the value of tail.next ??
would a fast/slow pointer to get to our desired point work here? how exactly would we implement that, though?
ya even i thought the same
I think that would work too, but in worst case for reaching at k-1 th node will have O(n) complexity, but in case of for loop implemented here the worst case is O(k)
even i thought same but if k is larger than len of linked list then it wont work
@@Prince-gt5ju just find the k mod len
Good explanation. But, some time can be saved by limiting the explanation of the problem.
k = k % len(linked_list).....right?
yes
I am the one who like this video for the 500 time
you should like it off number if times 😂..otherwise the like would go away
In short: 2 pointer
two passes
why cant we use reversals algorithm on this question?
you can also the method of converting in into a list, then using the rotate list method!
I don't understand, isn't the k = k%len part just an optimization? Without it, it should just be slow but should still work. But if you comment it out, there are test cases that fail. Why is that?
Edit: Oh I get it now. The formula for finding the break is length - k - 1. It's possible to have a huge K which sets it to negative, so you need the % to keep k smaller than length.
if k > total len of linked list, then we will do more rotations than usual but the result will be same. so we only need to do actual remainder of k shifts, which is k % len
can you please make a tutorial about python data struture !! That would me more helpful
That is literally his whole channel
the skip length is 5 - 2 = 3; while we need i < 3 - 1, which means i starts from 0, and 1, it only moves one step, not two steps, that's why i DON'T understand the length - k - 1part; Could anyone explain more?
it's k = 2 but index starts from 0 so hence minus
I THINK THE code is not working
Great video, line 26: cur.next = tail.next works as well :)
It does but for simplicity, None is preferred.