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; }
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.
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.
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
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
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.
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
@@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
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
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;
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.
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.
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
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;
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 ...?
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).
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;
}
just figured out and then found your comment 😅😅
i was wondering the same
Good job Bro
Yes I agree with you
Typo ke vajah se 2 baar video dekh liya mai 😂
i can surely say that this is the best linkedList course of all time
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.
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.
can u explain why cant I keep condition as while(even.next !=null), why to check if even is null or not..
@@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
bro odd .next is nothing but even
@@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
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
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.
Hlo bro ?? Have you given any interview yet? What are you currently doing?
@@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.
Marking my Day 21 of learning DSA. Thanks for the great course with clear in-depth explanation
Where did you finally end up bro !! I just want to know how you doing . I recently started solving Strivers_79.
one mistake
even = even->next instead of even=even->next->next
i was finding where this even.next.next is coming.... thanks for confirming
@@sanskarsawant6936 Same
exactly
ty
i dont know even a single thing about dsa, this is bcz of striver i got an amazing job, hats off to u
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
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..
@@faique509i cant build logic its tough to remember approaches
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
Raj bhai hats off to your dedication
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?
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.
i have just one word brilliant!
Understood... Superb bhaiya ❤
0(n)time complexity, understood thank you striver bhaiya
yesss
Thanks for making our concepts clear Striver
Understood Bhaiya , And i still watching all of your videos till the End.
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
could u pls explain this.
Sorry guys, I am not into coding field anymore and currently into other domain.
@@NotNewton23 👍👍
@@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
understood.. guru jee... thanks to my senior who suggested me to go through your videos
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
Amazing explanation. Loved it.
love the way you teach
Thanks Striver!!!
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
we need the article for rest of the linked list problems of A2Z sheet
just love your explaination
The great wall of dsa😍😍
i think even next should also be even.next rather than even.next.next
yes, i think by mistake it is written
you are right
You're absolutely correct!
No it will be even->next->next only
@@ravijha377 can you explain why ?
I liked it first thnaks for the course you are the best
Striver on 🔥
Awesome❤ explanation guru...
Lecture successfully completed on 27/11/2024 🔥🔥
Understood thankyou so much striver
Good explanation! Thanks :)
Thanks bhaiya...❤🎉
Undestood, thank you!
where can I get java code Solution for this problem....it's not in description
lecture 6 done,love u bhaiya
Understood bhaiya 🔥
great video bhaiya
OP explanation💯👌
UNDERSTOOD CLEARLY
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;
}
}
During DSA interview, can we use pen and paper to make approach? or will interviewer allow to use pen and paper ?
after linked why we write odd = odd.next but in even = even.next.next?
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;
What software you are using for drawing?
Lovely Explanation
What is the digital whiteboard that you use?
actually there is prblem oocuring after writing evehead=head.next still it can t connect it to the even with last index of odd
Why do we need evenhead? Why can’t we make last odd point to head.next which will logically be first even position?
thanks for this video
great explanation
Understood✅🔥🔥
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.
Thanks a lot striver, understood
Time complexity O(n/2) because the while loop will run max of n/2 times.
The time complexity of the optimal solution is O(N/2) and space complexity is O(1)
Edit: it was O(N)
hey striver....what if the last node is an odd one?
how will it justify that the odd is dependent on even?
In this case even going to be null which is taken care by the condition specified in while loop
i have a doubt the odd = odd->next->next instead of odd=odd->next
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.
What sincerity Striver. Respect Man
Peak Content !
Time Complexity O(N/2)
We can also include another case if its only 2 element Linked List then also we do nothing
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.
time complexity O 2(n/2)
space O(1)
thank you
I think it will be O(n) because we are traversing all the elements for even and odd.
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
Also, without edge case we get runtime error: if(head == null || head.next == null) return head;
solved without watching, kudos to previous videos base creation !!
Understood, thank you.
How can we do this using two while loops, one for even and one for odd?? I'm getting Runtime error!!
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;
}
Time Complexity=O(N/2)
Space Complexity=O(1)
Thankyou for your Lecture
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;
}
}
was able to solve by optimal meself!
small coreetcion while even node is pointing to
even = even->next;
Thanks A lot Bhaiya
Understood , Thank youu
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 ?
the link would already been updated. so odd->next would give you the third node instead of 2nd node
@@vipinms4864 yeah understood that already but thanks
Time Complexity - O(n/2)
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 ...?
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
Thanx bro
Understood 🎉
understood 👍
Understood, but one small doubt ,why didn't we consider the operations in the while loop as unit operations?
unit =1 and they are two operations
also you can see that you are traversing through every single element.
Understood Sir
thank you man
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).
no not possible
understood❤
Understood sir! :D
understood😄
UNDERSTOOD;
thank u sir
what if its an odd length linked list?
Nice one! :)
he knows where everyone make mistakes like o(n/2)
in pseudo code of optimal approach , it should be even=even.next instead of even=even.next.next in while loop.