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; }
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.
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.
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 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.
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
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 :)
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
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.
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
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
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.
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
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.
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).
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 ...?