Leetcode 61. Rotate List | Linked Lists | Rotate linked list by k places to right
Vložit
- čas přidán 10. 03. 2022
- Connect with me on LinkedIn : / alisha-parveen-80579850
Check out our other playlists:
Dynamic Programming:
• Dynamic Programming
Trees:
• Trees
Heaps and Maps:
• Heaps and Maps
Arrays and Maths:
• Arrays and Maths
Bit Manipulation:
• Bit Manipulation
Greedy Algorithms:
• Greedy Algorithms
Sorting and Searching:
• Sorting and Searching
Strings:
• Strings
Linked Lists:
• Linked Lists
Stack and Queues:
• Stacks and Queues
Two Pointers:
• Two pointers
Graphs, BFS, DFS:
• Graphs, DFS, BFS
Backtracking:
• Backtracking
Non- DSA playlists:
Probability:
• Probability
SQL-Basic Join functions:
• SQL - Basic JOIN Funct...
SQL-Basic Aggregate functions:
• SQL-Basic Aggregate Fu...
//reach the last element??
//last element -> head // 7->1
if(head==NULL || k==0)return head;
ListNode* temp = head;
int n = 0;
while(temp->next)
{
temp = temp->next;
n++; // 6
}
//temp = 7
temp->next = head; // 7->1
//n = 6
k = k%(n+1);
int jump = n-k; // 6-2 = 4
temp = head;
while(jump)
{
temp = temp->next;
jump--;
}
//temp -> 5
ListNode* returnhead = temp->next; // 6
temp->next = NULL;
return returnhead;
There is one change which can be done to reduce cognitive complexity of the code. While calculating list length, instead of checking temp->next != null, we can do temp != null. In that case, n will hold exact length of linked list, and not one less than actual length. So, we won't have to do K = K%(n+1). Instead, K = K%n will be enough. Correct me if I am wrong.
This was the only solution which was easy to understand, and without using any extra space in memory too. Thanks for the video.
Literally I had been stuck in this problem for last two hours but i don't give up and found your video. Thaks for kind of help
THANKS FOR THE SOLUTION🙂
the way you explain is just amazing thanks a lot
Very easy solution ! Thankyou so much didi ! I saw That k %= n + 1; part in array rotate question as well but now I got it why it is used. Thanks again 😊
Amazing Explaination Ever !!!
Osmmm... explanation
You explain so simple and easy way
Fast like an express train🎉🎉 thank you😊 short and crisp..
Outstanding explanation. Thank You so much.
Thank you for explaning , wonderful explanation ✨✨✨
Please daily upload 💞
please please please keep uploading videos like this
Thanku so muchhhh
Bht jaldi se samjha diya good work didi
thank you so much
Thank you so much
def rotate(self, head, k):
temp = head
while(temp.next):
temp = temp.next
temp.next = head
while k > 0:
temp = temp.next
k -=1
head = temp.next
temp.next = None
return head
Thanks. It was helpful
Thank u❣
Masha Allah Great job
Nice solution. It was good😇💫
Gajab ka samjaya h 🎉⭐
Thank you
perfect
Best 💯
nice explnation thank you so much can you explain the problem 396 leetcode problem please.
Nice explanation. But same logic will not work for left rotation. Could you explain that one also?
nice explanation and please upload cloned linkelist videos i searched in ur videos i cant find it
submit ho gaya didi ji
you are amazing teacher.. Please daily upload video will can help you subscribe your channel : )
nice explanation from where did you improve your problem solving skills
by coding on leetcode
@@yajatdhawan1865 aisa reply krega to zindagi me nahi hoga tere se coding
@@yajatdhawan1865 ok bhai
@@GhostRider.... accha tume batana hai na ye batao ki online assessment kese clear karu
@@yajatdhawan1865 ek striver youtuber h, unne DSA ki sheet bana rakhi h usse solve kar ke complete kar do fir tum assessment clear par paoge, us sheet me maximum questions covered h jo product based companies me puche jate h
HEADS OFF