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.
@aayushraj72908 ай бұрын
can u explain why cant I keep condition as while(even.next !=null), why to check if even is null or not..
@harshit.537 ай бұрын
@@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
@guttulavybhav10305 ай бұрын
bro odd .next is nothing but even
@nachikettiwary5628Ай бұрын
@@guttulavybhav1030 no bro, odd.next is odd.next.next . So when you do odd.next , it will go to the next odd place. Hope it helps
@Mrindia-k8n13 күн бұрын
na man it's wrong sorry but the first one is correct and the perfect one well u can do one thing if u can say odd.next==evenhead if u include this condition with the first one it will be more accurate
@edisane8763 Жыл бұрын
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 Жыл бұрын
just figured out and then found your comment 😅😅
@parvahuja761811 ай бұрын
i was wondering the same
@_PRANAYMATE10 ай бұрын
Good job Bro
@shaikkhizar813310 ай бұрын
Yes I agree with you
@imamansoni10 ай бұрын
Typo ke vajah se 2 baar video dekh liya mai 😂
@amangoyal83419 ай бұрын
i can surely say that this is the best linkedList course of all time
@champ-kx9lb5 ай бұрын
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.
@manishmahajan60945 ай бұрын
Marking my Day 21 of learning DSA. Thanks for the great course with clear in-depth explanation
@AmanSharma-xy1qm Жыл бұрын
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.
@touchskyfacts13919 ай бұрын
Hlo bro ?? Have you given any interview yet? What are you currently doing?
@AmanSharma-xy1qm9 ай бұрын
@@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.
@sourabhtiwari51379 ай бұрын
i dont know even a single thing about dsa, this is bcz of striver i got an amazing job, hats off to u
@ashmitshelke14445 ай бұрын
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
@faique5095 ай бұрын
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..
@retromania5264 ай бұрын
@@faique509i cant build logic its tough to remember approaches
@CodeNinja18 Жыл бұрын
one mistake even = even->next instead of even=even->next->next
@sanskarsawant6936 Жыл бұрын
i was finding where this even.next.next is coming.... thanks for confirming
@ritochitghosh17538 ай бұрын
@@sanskarsawant6936 Same
@THOSHI-cn6hg5 ай бұрын
exactly
@user-zv4cr5xd1gАй бұрын
ty
@bhavyasharma3500 Жыл бұрын
Raj bhai hats off to your dedication
@pushankarmakar17839 ай бұрын
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
@__PiyushMittalАй бұрын
Understood Bhaiya , And i still watching all of your videos till the End.
@nitishkumarram8978 Жыл бұрын
0(n)time complexity, understood thank you striver bhaiya
@succesmantramotivational879510 ай бұрын
yesss
@shashamnk2525 Жыл бұрын
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.
@NotNewton23 Жыл бұрын
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-m8d4 ай бұрын
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
@rumiNITPatna22 күн бұрын
could u pls explain this.
@NotNewton2322 күн бұрын
Sorry guys, I am not into coding field anymore and currently into other domain.
@rumiNITPatna22 күн бұрын
@@NotNewton23 👍👍
@BharathMurugan309 күн бұрын
@@rumiNITPatna Since we perform the operation odd->next = odd->next->next , odd->next is already linked to the next odd place. So, if we want to move odd to the next position, it is odd->next, because odd->next is currently pointing to odd->next->next. If you still didn't understand, you can draw a linkedlist of four elements and try linking the elements with this operation
@ritikshandilya70758 ай бұрын
Thanks for making our concepts clear Striver
@prathameshjadhav294210 ай бұрын
Understood... Superb bhaiya ❤
@saketjaiswal34317 ай бұрын
understood.. guru jee... thanks to my senior who suggested me to go through your videos
@ketonesgaming1121 Жыл бұрын
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
@chetandatta90518 ай бұрын
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?
@harshitjaiswal943911 ай бұрын
Amazing explanation. Loved it.
@BhavyaJain-qz8jg6 ай бұрын
solved without watching, kudos to previous videos base creation !!
@zainiqbal79908 ай бұрын
What sincerity Striver. Respect Man
@snehashisratna9074 Жыл бұрын
love the way you teach
@rajmohitkumar3644 Жыл бұрын
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; } }
@striverdaaadi Жыл бұрын
The great wall of dsa😍😍
@veedofficial Жыл бұрын
I liked it first thnaks for the course you are the best
@Vvvvviieiei123 Жыл бұрын
lecture 6 done,love u bhaiya
@vaibhavgupta090110 ай бұрын
Understood thankyou so much striver
@samarthpai535920 күн бұрын
The time complexity of the optimal solution is O(N/2) and space complexity is O(1) Edit: it was O(N)
@patilrajesh2500Ай бұрын
Lecture successfully completed on 27/11/2024 🔥🔥
@nandeesh_2005 Жыл бұрын
Awesome❤ explanation guru...
@vishakhajoshi277Ай бұрын
Good explanation! Thanks :)
@PradipKumar-zi2pz2 ай бұрын
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
@lovetiwadiai9909 Жыл бұрын
i think even next should also be even.next rather than even.next.next
@souradeepchowdhury667 Жыл бұрын
yes, i think by mistake it is written
@tanay776 Жыл бұрын
you are right
@muwaffaqelbadawi Жыл бұрын
You're absolutely correct!
@ravijha377 Жыл бұрын
No it will be even->next->next only
@Raj-iw9hd Жыл бұрын
@@ravijha377 can you explain why ?
@shaikkhizar813310 ай бұрын
Time Complexity=O(N/2) Space Complexity=O(1) Thankyou for your Lecture
@diptanjankar9848 Жыл бұрын
Striver on 🔥
@ishanchakraborty696 ай бұрын
just love your explaination
@saravanansubbiah1649 ай бұрын
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.
@adebisisheriff15911 ай бұрын
Thanks Striver!!!
@Tanveermd7 Жыл бұрын
we need the article for rest of the linked list problems of A2Z sheet
@rushidesai28369 ай бұрын
Time complexity O(n/2) because the while loop will run max of n/2 times.
@toushikbanik84525 ай бұрын
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.
@aronkumar-v8i10 ай бұрын
time complexity O 2(n/2) space O(1)
@oyeshxrme5 ай бұрын
great video bhaiya
@nptel1punith9297 ай бұрын
Understood bhaiya 🔥
@hardikpatel3527 ай бұрын
Thanks a lot striver, understood
@mhdvlogs21303 ай бұрын
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
@arnabkundu16486 ай бұрын
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.
@jd1901-s3 ай бұрын
OP explanation💯👌
@rajkumarvb5197 Жыл бұрын
Undestood, thank you!
@adityalingwal60298 ай бұрын
Lovely Explanation
@shreyxnsh.1411 ай бұрын
Correct solution that beats 100%: class Solution { public: 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; } }; Time complexity will be O(N) as we are traversing through each and every element
@rishabhbajpai64810 ай бұрын
UNDERSTOOD CLEARLY
@bhashasinha58396 ай бұрын
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; }
@nishigandhagirishdandekar11314 ай бұрын
I think it will be O(n) because we are traversing all the elements for even and odd.
@Harsh-jc2bz4 ай бұрын
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; } }
@ankushladani49611 ай бұрын
Thanks bhaiya...❤🎉
@akshathlm784910 ай бұрын
Time Complexity O(N/2)
@Mighty_kaaju10 ай бұрын
great explanation
@yashendrabadal47762 ай бұрын
Peak Content !
@arthonuy72117 ай бұрын
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; }
@SurajKumar-ku1lg8 ай бұрын
he knows where everyone make mistakes like o(n/2)
@MultiFacebookers5 ай бұрын
Why do we need evenhead? Why can’t we make last odd point to head.next which will logically be first even position?
@LAVLESHSINGH-s1r7 ай бұрын
actually there is prblem oocuring after writing evehead=head.next still it can t connect it to the even with last index of odd
where can I get java code Solution for this problem....it's not in description
@pankajsunal98199 ай бұрын
Understood.Thanks for the wonderful lecture.
@s_savarnaya7 ай бұрын
i have a doubt the odd = odd->next->next instead of odd=odd->next
@ManishLakkavatri Жыл бұрын
Understood, but one small doubt ,why didn't we consider the operations in the while loop as unit operations?
@shreyxnsh.1411 ай бұрын
unit =1 and they are two operations
@shreyxnsh.1411 ай бұрын
also you can see that you are traversing through every single element.
@neerajmalhotra82779 күн бұрын
What software you are using for drawing?
@NazeerBashaShaik8 ай бұрын
Understood, thank you.
@vidhiarora49955 ай бұрын
hey striver....what if the last node is an odd one? how will it justify that the odd is dependent on even?
@arjunk044 ай бұрын
In this case even going to be null which is taken care by the condition specified in while loop
@jogeshsoni22057 ай бұрын
in pseudo code of optimal approach , it should be even=even.next instead of even=even.next.next in while loop.
@ksnayeem2743Ай бұрын
after linked why we write odd = odd.next but in even = even.next.next?
@square-kstudios956111 ай бұрын
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).
@anuragprasad611610 ай бұрын
no not possible
@hulkstar08086 ай бұрын
Understood , Thank youu
@Aditya_69968 ай бұрын
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 ?
@vipinms48648 ай бұрын
the link would already been updated. so odd->next would give you the third node instead of 2nd node
@Aditya_69968 ай бұрын
@@vipinms4864 yeah understood that already but thanks
@DeepakPatel-d5v9 ай бұрын
Thanks A lot Bhaiya
@hardikpatel3527 ай бұрын
TC = O(N)........... SC= O(1)
@fitnessforlifetamil393610 ай бұрын
Understood 🎉
@shubhamrathod924911 күн бұрын
11:36
@ANNAM-dv5dy Жыл бұрын
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 ...?
@pavanilakshmiaishwarya89629 ай бұрын
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
@TON-108 Жыл бұрын
How can we do this using two while loops, one for even and one for odd?? I'm getting Runtime error!!