Тёмный

L6. Odd Even Linked List | Multiple Approaches 

take U forward
Подписаться 693 тыс.
Просмотров 97 тыс.
50% 1

Опубликовано:

 

16 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 237   
@edisane8763
@edisane8763 10 месяцев назад
Great video just a small correction that it will be even = even.next Here's the full code for leetcode: ListNode* oddEvenList(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode* odd = head; ListNode* even = head->next; ListNode* evenHead = head->next; while(even!=NULL && even->next!=NULL){ odd->next = odd->next->next; even->next = even->next->next; odd = odd->next; even = even->next; } odd->next = evenHead; return head; }
@NotNewton23
@NotNewton23 9 месяцев назад
just figured out and then found your comment 😅😅
@parvahuja7618
@parvahuja7618 8 месяцев назад
i was wondering the same
@_PRANAYMATE
@_PRANAYMATE 7 месяцев назад
Good job Bro
@shaikkhizar8133
@shaikkhizar8133 7 месяцев назад
Yes I agree with you
@imamansoni
@imamansoni 7 месяцев назад
Typo ke vajah se 2 baar video dekh liya mai 😂
@mdtanveeransari3461
@mdtanveeransari3461 9 месяцев назад
if anyone is facing any issue with the while condition ie, while(even != NULL && even -> next != NULL) you can use instead, while(odd -> next != NULL && even -> next != NULL) i hope it helps you , happy coding.
@aayushraj7290
@aayushraj7290 5 месяцев назад
can u explain why cant I keep condition as while(even.next !=null), why to check if even is null or not..
@harshit.53
@harshit.53 4 месяца назад
@@aayushraj7290 take an odd length LL and do the dry run of the code, you will understand why you need to check if even != NULL
@guttulavybhav1030
@guttulavybhav1030 3 месяца назад
bro odd .next is nothing but even
@champ-kx9lb
@champ-kx9lb 2 месяца назад
well explained! There is slight error in the brute force code that in while loop at the last line in loop even=even.next; so if anyone find its confusing can be helped from this.
@manishmahajan6094
@manishmahajan6094 2 месяца назад
Marking my Day 21 of learning DSA. Thanks for the great course with clear in-depth explanation
@AmanSharma-xy1qm
@AmanSharma-xy1qm 10 месяцев назад
All the video lectures and the articles helped me a lot to gain confidence in DSA and will be helping me in the interviews. Thank you Striver bhaiya for bringing such amazing content for free.
@touchskyfacts1391
@touchskyfacts1391 7 месяцев назад
Hlo bro ?? Have you given any interview yet? What are you currently doing?
@AmanSharma-xy1qm
@AmanSharma-xy1qm 6 месяцев назад
@@touchskyfacts1391 Yes i gave approx 7 interview and got selected in 3 of them, in one they were offering me QA engineer role due to different tech stacks so i denied, in 2 of them i choose the 2nd which was 4.5 LPA. In the beginning i wasn't good at DSA so i got rejected then i started Striver A to Z and learned String, Array, Matrix, LinkedList, Stacks, Queue and basic of advance DS. that was enough to get this.
@amangoyal8341
@amangoyal8341 6 месяцев назад
i can surely say that this is the best linkedList course of all time
@CodeNinja18
@CodeNinja18 10 месяцев назад
one mistake even = even->next instead of even=even->next->next
@sanskarsawant6936
@sanskarsawant6936 10 месяцев назад
i was finding where this even.next.next is coming.... thanks for confirming
@ritochitghosh1753
@ritochitghosh1753 5 месяцев назад
@@sanskarsawant6936 Same
@THOSHI-cn6hg
@THOSHI-cn6hg 2 месяца назад
exactly
@bhavyasharma3500
@bhavyasharma3500 10 месяцев назад
Raj bhai hats off to your dedication
@ashmitshelke1444
@ashmitshelke1444 2 месяца назад
I solved this with the optimal approch without looking at sol for the first time, thanks Striver for teaching all the intutions and logical process everytime
@faique509
@faique509 2 месяца назад
was going to comment the same..i think we should do the dsa together..if you are interested let me know..or u can drop your insta..
@retromania526
@retromania526 2 месяца назад
​@@faique509i cant build logic its tough to remember approaches
@sourabhtiwari5137
@sourabhtiwari5137 6 месяцев назад
i dont know even a single thing about dsa, this is bcz of striver i got an amazing job, hats off to u
@NotNewton23
@NotNewton23 9 месяцев назад
hey guys, in the optimal solution, inside while loop, we have already set the next for both even and odd, so to go next even and odd use even= even->next ; odd= odd->next respectively I spend my 20-30 minutes realising this lol :)
@GODLUCIFER-m8d
@GODLUCIFER-m8d Месяц назад
still i cant understand. could u help me to clear this i used chatbot and some stuffs still no use, i think my brain is twisted to understand this concept
@pushankarmakar1783
@pushankarmakar1783 6 месяцев назад
O(n/2) space complexity. i was wrong. it'll be O(n) because we need to consider the operations taking place twice inside the array
@BhavyaJain-qz8jg
@BhavyaJain-qz8jg 3 месяца назад
solved without watching, kudos to previous videos base creation !!
@shashamnk2525
@shashamnk2525 10 месяцев назад
We would need to check if head !=null before initializing even as head.next and while updating even , even.next should be good enough , even.next.next would land us on an odd node.
@chetandatta9051
@chetandatta9051 5 месяцев назад
Why is the time complexity O(n/2) * 2? It should be O(n/2) regardless of the number of operations performed inside the loop, right?
@ketonesgaming1121
@ketonesgaming1121 9 месяцев назад
line even = even.next.next; inside the while loop. When updating the even pointer, you should check if even.next is not null before trying to access even.next.next. Small correction to be made
@ritikshandilya7075
@ritikshandilya7075 5 месяцев назад
Thanks for making our concepts clear Striver
@nitishkumarram8978
@nitishkumarram8978 9 месяцев назад
0(n)time complexity, understood thank you striver bhaiya
@succesmantramotivational8795
@succesmantramotivational8795 7 месяцев назад
yesss
@PradipKumar-zi2pz
@PradipKumar-zi2pz 18 часов назад
We can have this approach as well. def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None or head.next is None or head.next.next is None: return head odd = head even = head.next while even and even.next: temp = even.next.next oddnext = odd.next odd.next = even.next odd = even.next odd.next = oddnext even.next = temp even = temp return head
@saketjaiswal3431
@saketjaiswal3431 4 месяца назад
understood.. guru jee... thanks to my senior who suggested me to go through your videos
@lovetiwadiai9909
@lovetiwadiai9909 10 месяцев назад
i think even next should also be even.next rather than even.next.next
@souradeepchowdhury667
@souradeepchowdhury667 10 месяцев назад
yes, i think by mistake it is written
@tanay776
@tanay776 10 месяцев назад
you are right
@muwaffaqelbadawi
@muwaffaqelbadawi 10 месяцев назад
You're absolutely correct!
@ravijha377
@ravijha377 10 месяцев назад
No it will be even->next->next only
@Raj-iw9hd
@Raj-iw9hd 10 месяцев назад
@@ravijha377 can you explain why ?
@PradipKumar-zi2pz
@PradipKumar-zi2pz 19 часов назад
During DSA interview, can we use pen and paper to make approach? or will interviewer allow to use pen and paper ?
@prathameshjadhav2942
@prathameshjadhav2942 7 месяцев назад
Understood... Superb bhaiya ❤
@mdtanveeransari3461
@mdtanveeransari3461 9 месяцев назад
we need the article for rest of the linked list problems of A2Z sheet
@harshitjaiswal9439
@harshitjaiswal9439 8 месяцев назад
Amazing explanation. Loved it.
@arnabkundu1648
@arnabkundu1648 4 месяца назад
Time Complexity = O(N) As we are traversing through odd and even Nodes. Though it seems O(N/2) but in every loop we are traversing twice(for odd and for even indexed nodes), so Time Complexity will be O(N) I think. If I am wrong please point it out.
@adebisisheriff159
@adebisisheriff159 8 месяцев назад
Thanks Striver!!!
@snehashisratna9074
@snehashisratna9074 10 месяцев назад
love the way you teach
@veedofficial
@veedofficial 10 месяцев назад
I liked it first thnaks for the course you are the best
@toushikbanik8452
@toushikbanik8452 2 месяца назад
I got it but you should have done a dry run o an odd LL also. but no prob i did that by my self. great explanation bro.
@ishanchakraborty69
@ishanchakraborty69 3 месяца назад
just love your explaination
@rushidesai2836
@rushidesai2836 6 месяцев назад
Time complexity O(n/2) because the while loop will run max of n/2 times.
@mhdvlogs2130
@mhdvlogs2130 9 дней назад
inside the while loop instead of even=even.next.next; it will be even=even.next; bcz we r setting/updating next even node so that next time loop run then we can connect this node next to the upcoming even node, that is (even.next=even.next.next;) thnk you ~~Mhd
@vaibhavgupta0901
@vaibhavgupta0901 8 месяцев назад
Understood thankyou so much striver
@rajmohitkumar3644
@rajmohitkumar3644 10 месяцев назад
Here is the java program class Solution { public ListNode oddEvenList(ListNode head) { //edge case if(head==null || head.next==null) return head; ListNode odd=head; ListNode even=head.next; ListNode evenHead=head.next; while(even !=null && even.next!=null){ odd.next=odd.next.next; even.next=even.next.next; odd=odd.next; even=even.next; } odd.next=evenHead; return head; } }
@nileshdesai5218
@nileshdesai5218 3 месяца назад
Dude I had solved this que using convert LL into array and performed operation on array and at the end create new LL and return it.
@Jaydoshi77
@Jaydoshi77 27 дней назад
OP explanation💯👌
@nishigandhagirishdandekar1131
@nishigandhagirishdandekar1131 Месяц назад
I think it will be O(n) because we are traversing all the elements for even and odd.
@oyeesharme
@oyeesharme 2 месяца назад
great video bhaiya
@Vvvvviieiei123
@Vvvvviieiei123 10 месяцев назад
lecture 6 done,love u bhaiya
@nandeesh_2005
@nandeesh_2005 9 месяцев назад
Awesome❤ explanation guru...
@diptanjankar9848
@diptanjankar9848 10 месяцев назад
Striver on 🔥
@adityalingwal6029
@adityalingwal6029 5 месяцев назад
Lovely Explanation
@saravanansubbiah164
@saravanansubbiah164 6 месяцев назад
Time complexity : O(N) -> because we're traversing the whole LL Space Complexity : O(1) -> we're not using any auxillary space for solving the problem.
@striverdaaadi
@striverdaaadi 9 месяцев назад
The great wall of dsa😍😍
@rishabhbajpai648
@rishabhbajpai648 7 месяцев назад
UNDERSTOOD CLEARLY
@rajkumarvb5197
@rajkumarvb5197 9 месяцев назад
Undestood, thank you!
@vidhiarora4995
@vidhiarora4995 2 месяца назад
hey striver....what if the last node is an odd one? how will it justify that the odd is dependent on even?
@arjunk04
@arjunk04 Месяц назад
In this case even going to be null which is taken care by the condition specified in while loop
@Mighty_kaaju
@Mighty_kaaju 7 месяцев назад
great explanation
@hardikpatel352
@hardikpatel352 5 месяцев назад
Thanks a lot striver, understood
@square-kstudios9561
@square-kstudios9561 9 месяцев назад
Striver, is it even possible to run 2 separate loops? Because after running the first loop, the oddHead's next.next is not pointing to the next odd, but its pointing to the next even node (already altered in the first loop).
@anuragprasad6116
@anuragprasad6116 8 месяцев назад
no not possible
@LAVLESHSINGH-s1r
@LAVLESHSINGH-s1r 4 месяца назад
actually there is prblem oocuring after writing evehead=head.next still it can t connect it to the even with last index of odd
@akshathlm7849
@akshathlm7849 8 месяцев назад
Time Complexity O(N/2)
@ManishLakkavatri
@ManishLakkavatri 10 месяцев назад
Understood, but one small doubt ,why didn't we consider the operations in the while loop as unit operations?
@shreyxnsh.14
@shreyxnsh.14 8 месяцев назад
unit =1 and they are two operations
@shreyxnsh.14
@shreyxnsh.14 8 месяцев назад
also you can see that you are traversing through every single element.
@nptel1punith929
@nptel1punith929 5 месяцев назад
Understood bhaiya 🔥
@MultiFacebookers
@MultiFacebookers 3 месяца назад
Why do we need evenhead? Why can’t we make last odd point to head.next which will logically be first even position?
@harshitkhandelwal2661
@harshitkhandelwal2661 4 месяца назад
We can also include another case if its only 2 element Linked List then also we do nothing
@NazeerBashaShaik
@NazeerBashaShaik 5 месяцев назад
Understood, thank you.
@ankushladani496
@ankushladani496 8 месяцев назад
Thanks bhaiya...❤🎉
@aronkumar-v8i
@aronkumar-v8i 7 месяцев назад
time complexity O 2(n/2) space O(1)
@hulkstar0808
@hulkstar0808 3 месяца назад
Understood , Thank youu
@DeepakPatel-d5v
@DeepakPatel-d5v 6 месяцев назад
Thanks A lot Bhaiya
@moksh7130
@moksh7130 6 дней назад
Understood sir! :D
@sunayanabhowmik9690
@sunayanabhowmik9690 3 месяца назад
Also, without edge case we get runtime error: if(head == null || head.next == null) return head;
@056_harshitagarwal8
@056_harshitagarwal8 6 месяцев назад
where can I get java code Solution for this problem....it's not in description
@s_savarnaya
@s_savarnaya 4 месяца назад
i have a doubt the odd = odd->next->next instead of odd=odd->next
@DhanushS-we6ql
@DhanushS-we6ql 12 дней назад
small coreetcion while even node is pointing to even = even->next;
@sai-cz9lm
@sai-cz9lm 3 месяца назад
Understood
@aneeketvispute5063
@aneeketvispute5063 2 месяца назад
can we do while(temp.next != null && temp.next.next != null){ temp = temp.next.next; } Like hare as it was also taking 2 jumps
@ParthDabhiOfficial
@ParthDabhiOfficial 2 месяца назад
UNDERSTOOD
@Harsh-jc2bz
@Harsh-jc2bz 2 месяца назад
did it by myself Java: class Solution { public ListNode oddEvenList(ListNode head) { if(head==null)return head; ListNode odd=head; ListNode even=head.next,temp=even; while(temp!=null){ if(odd.next.next==null)break; odd.next=odd.next.next; odd=odd.next; temp.next=temp.next.next; temp=temp.next; } odd.next=even; return head; } }
@jogeshsoni2205
@jogeshsoni2205 4 месяца назад
in pseudo code of optimal approach , it should be even=even.next instead of even=even.next.next in while loop.
@yeshagarwal431
@yeshagarwal431 4 месяца назад
understood
@YourCodeVerse
@YourCodeVerse 8 месяцев назад
Understood✅🔥🔥
@arthonuy7211
@arthonuy7211 4 месяца назад
can also be done using dummyNode method : ListNode* oddEvenList(ListNode* head) { if(head==NULL||head->next==NULL){ return head; } ListNode *temp=head; ListNode *dummyListNode=new ListNode(0); ListNode *evenTemp=dummyListNode; ListNode *prev=NULL; int count=1; while(temp!=NULL){ if(count&1){ prev=temp; temp=temp->next; }else{ prev->next=temp->next; temp->next=NULL; evenTemp->next=temp; evenTemp=evenTemp->next; temp=prev->next; } count++; } prev->next=dummyListNode->next; return head; }
@42owaishansari90
@42owaishansari90 8 месяцев назад
Understood Sir
@zainiqbal7990
@zainiqbal7990 5 месяцев назад
What sincerity Striver. Respect Man
@DeadPoolx1712
@DeadPoolx1712 Месяц назад
UNDERSTOOD;
@rohanlambar4725
@rohanlambar4725 6 месяцев назад
Time Complexity - O(n/2)
@tanayagrawal912
@tanayagrawal912 4 месяца назад
Thanku bhaiya
@nishant4595
@nishant4595 Месяц назад
was able to solve by optimal meself!
@bhashasinha5839
@bhashasinha5839 3 месяца назад
this is the java code * * class Solution { public ListNode oddEvenList(ListNode head) { if(head==null ||head.next==null){ return head; } ListNode odd=head; ListNode even=head.next; ListNode connector=head.next; while(even!=null && even.next!=null){ odd.next=odd.next.next; even.next=even.next.next; odd=odd.next; even=even.next; } odd.next=connector; return head; }
@shaikkhizar8133
@shaikkhizar8133 7 месяцев назад
Time Complexity=O(N/2) Space Complexity=O(1) Thankyou for your Lecture
@alexcarry1102
@alexcarry1102 7 месяцев назад
understood 👍
@ANNAM-dv5dy
@ANNAM-dv5dy 9 месяцев назад
somene explain me my doubt ? odd->next = odd->next->next; even->next = even->next->next; odd = odd->next; even = even->next; here in this first 2 lines of code , odd pointer moves to odd places and even pointer moves to even places so that even connects to even and odd connects to odd , but in the next two lines odd pointer moves to one step ahead and also even pointer moves to one step ahead i.e odd moves to even and even moves to odd ...?
@pavanilakshmiaishwarya8962
@pavanilakshmiaishwarya8962 7 месяцев назад
Yeah it is wrong in the video. It has to be: odd->next = odd->next->next even->next = odd->next->next odd = odd->next even = even->next
@ramkrishnapatil2105
@ramkrishnapatil2105 21 день назад
understood😄
@nikhilkumarjamwal5322
@nikhilkumarjamwal5322 10 месяцев назад
Nice one! :)
@shivisingh9975
@shivisingh9975 5 месяцев назад
Understood! sir
@subee128
@subee128 10 месяцев назад
Thanks
@fitnessforlifetamil3936
@fitnessforlifetamil3936 7 месяцев назад
Understood 🎉
@Aditya_6996
@Aditya_6996 6 месяцев назад
why not connect odd list with even list using "odd->next=head->next" instead of "odd->next=evenHead" ? I mean what would logically go wrong ?
@vipinms4864
@vipinms4864 6 месяцев назад
the link would already been updated. so odd->next would give you the third node instead of 2nd node
@Aditya_6996
@Aditya_6996 6 месяцев назад
​@@vipinms4864 yeah understood that already but thanks
@khalasianiket816
@khalasianiket816 3 месяца назад
understood❤
@viveksoni6823
@viveksoni6823 5 месяцев назад
why it's not working for using 2 loops separately for even and odd nodes.I wrote this ListNode* odd = head; ListNode* even = head->next; ListNode* evenhead = even; while(odd!=NULL && odd->next!=NULL && odd->next->next!=NULL) { odd->next = odd->next->next; odd = odd->next; } while(even!=NULL && even->next!=NULL && even->next->next!=NULL) { even->next = even->next->next; even = even->next; } odd->next = evenhead; return head;
@SurajKumar-ku1lg
@SurajKumar-ku1lg 5 месяцев назад
he knows where everyone make mistakes like o(n/2)
@MohitKumar-o3l1u
@MohitKumar-o3l1u 3 месяца назад
Understood !!
@notyash3007
@notyash3007 3 месяца назад
Ahh, i was using 2 while loops for odd and even so it was giving TLE.
@adilkevin6220
@adilkevin6220 8 месяцев назад
Next series on strings or cp
@tolulopemalomo8922
@tolulopemalomo8922 6 месяцев назад
Understood!
@TON-108
@TON-108 9 месяцев назад
How can we do this using two while loops, one for even and one for odd?? I'm getting Runtime error!!
@pravinthakur7791
@pravinthakur7791 6 месяцев назад
Node* segregateEvenOdd(Node* head) { // Write your code here if(head == nullptr || head->next == nullptr) return head; stackst; Node* temp = head; Node* prev = nullptr; Node* firstOddNode = nullptr; if((temp->data & 1) == 0) st.push(temp); while(temp != nullptr) { if((temp->data & 1) == 0 && prev != nullptr) { st.push(temp); while(temp->next != nullptr && (temp->next->data & 1) == 0) { st.push(temp->next); temp = temp->next; } prev->next = temp->next; } if((temp->data&1) && firstOddNode == nullptr) firstOddNode = temp; prev = temp; temp = temp->next; } Node* lastEvenNode = nullptr; Node* newHead = nullptr; while(!st.empty()) { if (newHead == nullptr) { newHead = st.top(); lastEvenNode = st.top(); } else { st.top()->next = newHead; newHead = st.top(); } st.pop(); } lastEvenNode->next = firstOddNode; return newHead; }
@nehabharti_1750
@nehabharti_1750 4 месяца назад
Understood..!!
Далее
10 FREE DEAD BOXES?! #deadgame
02:26
Просмотров 7 млн
НИКИТА ПОДСТАВИЛ ДЖОНИ 😡
01:00
How I would learn Leetcode if I could start over
18:03
Просмотров 607 тыс.
L9. Reverse a LinkedList | Iterative and Recursive
32:42
L5. Add 2 numbers in LinkedList | Dummy Node Approach
14:48
LeetCode was HARD until I Learned these 15 Patterns
13:00
10 FREE DEAD BOXES?! #deadgame
02:26
Просмотров 7 млн