L6. Odd Even Linked List | Multiple Approaches

Поделиться
HTML-код
  • Опубликовано: 27 янв 2025

Комментарии • 286

  • @edisane8763
    @edisane8763 Год назад +219

    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 Год назад +12

      just figured out and then found your comment 😅😅

    • @parvahuja7618
      @parvahuja7618 Год назад +5

      i was wondering the same

    • @_PRANAYMATE
      @_PRANAYMATE 11 месяцев назад

      Good job Bro

    • @shaikkhizar8133
      @shaikkhizar8133 10 месяцев назад

      Yes I agree with you

    • @imamansoni
      @imamansoni 10 месяцев назад +2

      Typo ke vajah se 2 baar video dekh liya mai 😂

  • @amangoyal8341
    @amangoyal8341 10 месяцев назад +7

    i can surely say that this is the best linkedList course of all time

  • @champ-kx9lb
    @champ-kx9lb 5 месяцев назад +25

    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.

  • @Tanveermd7
    @Tanveermd7 Год назад +68

    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 8 месяцев назад +1

      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 8 месяцев назад +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 6 месяцев назад +9

      bro odd .next is nothing but even

    • @nachikettiwary5628
      @nachikettiwary5628 Месяц назад +1

      @@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-k8n
      @Mrindia-k8n Месяц назад

      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

  • @AmanSharma-xy1qm
    @AmanSharma-xy1qm Год назад +14

    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 10 месяцев назад

      Hlo bro ?? Have you given any interview yet? What are you currently doing?

    • @AmanSharma-xy1qm
      @AmanSharma-xy1qm 10 месяцев назад

      @@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.

  • @manishmahajan6094
    @manishmahajan6094 5 месяцев назад +4

    Marking my Day 21 of learning DSA. Thanks for the great course with clear in-depth explanation

    • @Hemanth338-j1m
      @Hemanth338-j1m 10 дней назад

      Where did you finally end up bro !! I just want to know how you doing . I recently started solving Strivers_79.

  • @CodeNinja18
    @CodeNinja18 Год назад +59

    one mistake
    even = even->next instead of even=even->next->next

  • @sourabhtiwari5137
    @sourabhtiwari5137 9 месяцев назад

    i dont know even a single thing about dsa, this is bcz of striver i got an amazing job, hats off to u

  • @ashmitshelke1444
    @ashmitshelke1444 6 месяцев назад +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 6 месяцев назад +1

      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 5 месяцев назад

      ​@@faique509i cant build logic its tough to remember approaches

  • @pushankarmakar1783
    @pushankarmakar1783 10 месяцев назад +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

  • @bhavyasharma3500
    @bhavyasharma3500 Год назад +5

    Raj bhai hats off to your dedication

  • @chetandatta9051
    @chetandatta9051 9 месяцев назад +6

    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?

  • @shashamnk2525
    @shashamnk2525 Год назад +11

    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.

  • @NidhiKumari-vu3oj
    @NidhiKumari-vu3oj 7 дней назад

    i have just one word brilliant!

  • @prathamesh009m
    @prathamesh009m 10 месяцев назад

    Understood... Superb bhaiya ❤

  • @nitishkumarram8978
    @nitishkumarram8978 Год назад +3

    0(n)time complexity, understood thank you striver bhaiya

  • @ritikshandilya7075
    @ritikshandilya7075 9 месяцев назад +1

    Thanks for making our concepts clear Striver

  • @__PiyushMittal
    @__PiyushMittal 2 месяца назад

    Understood Bhaiya , And i still watching all of your videos till the End.

  • @NotNewton23
    @NotNewton23 Год назад +17

    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 4 месяца назад

      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
      @rumiNITPatna Месяц назад

      could u pls explain this.

    • @NotNewton23
      @NotNewton23 Месяц назад +1

      Sorry guys, I am not into coding field anymore and currently into other domain.

    • @rumiNITPatna
      @rumiNITPatna Месяц назад

      @@NotNewton23 👍👍

    • @BharathMurugan30
      @BharathMurugan30 28 дней назад +1

      @@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

  • @saketjaiswal3431
    @saketjaiswal3431 7 месяцев назад

    understood.. guru jee... thanks to my senior who suggested me to go through your videos

  • @ketonesgaming1121
    @ketonesgaming1121 Год назад +4

    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

  • @harshitjaiswal9439
    @harshitjaiswal9439 Год назад +1

    Amazing explanation. Loved it.

  • @snehashisratna9074
    @snehashisratna9074 Год назад +1

    love the way you teach

  • @adebisisheriff159
    @adebisisheriff159 11 месяцев назад +1

    Thanks Striver!!!

  • @PradipKumar-zi2pz
    @PradipKumar-zi2pz 3 месяца назад

    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

  • @Tanveermd7
    @Tanveermd7 Год назад +2

    we need the article for rest of the linked list problems of A2Z sheet

  • @ishanchakraborty69
    @ishanchakraborty69 6 месяцев назад

    just love your explaination

  • @striverdaaadi
    @striverdaaadi Год назад

    The great wall of dsa😍😍

  • @lovetiwadiai9909
    @lovetiwadiai9909 Год назад +20

    i think even next should also be even.next rather than even.next.next

  • @veedofficial
    @veedofficial Год назад +1

    I liked it first thnaks for the course you are the best

  • @diptanjankar9848
    @diptanjankar9848 Год назад +2

    Striver on 🔥

  • @nandeesh_2005
    @nandeesh_2005 Год назад

    Awesome❤ explanation guru...

  • @patilrajesh2500
    @patilrajesh2500 2 месяца назад +1

    Lecture successfully completed on 27/11/2024 🔥🔥

  • @vaibhavgupta0901
    @vaibhavgupta0901 11 месяцев назад

    Understood thankyou so much striver

  • @vishakhajoshi277
    @vishakhajoshi277 2 месяца назад

    Good explanation! Thanks :)

  • @ankushladani496
    @ankushladani496 Год назад

    Thanks bhaiya...❤🎉

  • @rajkumarvb5197
    @rajkumarvb5197 Год назад

    Undestood, thank you!

  • @056_harshitagarwal8
    @056_harshitagarwal8 9 месяцев назад

    where can I get java code Solution for this problem....it's not in description

  • @Vvvvviieiei123
    @Vvvvviieiei123 Год назад

    lecture 6 done,love u bhaiya

  • @nptel1punith929
    @nptel1punith929 8 месяцев назад

    Understood bhaiya 🔥

  • @oyeshxrme
    @oyeshxrme 6 месяцев назад

    great video bhaiya

  • @jd1901-s
    @jd1901-s 4 месяца назад

    OP explanation💯👌

  • @rishabhbajpai648
    @rishabhbajpai648 10 месяцев назад

    UNDERSTOOD CLEARLY

  • @rajmohitkumar3644
    @rajmohitkumar3644 Год назад +4

    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;

    }
    }

  • @PradipKumar-zi2pz
    @PradipKumar-zi2pz 3 месяца назад

    During DSA interview, can we use pen and paper to make approach? or will interviewer allow to use pen and paper ?

  • @ksnayeem2743
    @ksnayeem2743 2 месяца назад

    after linked why we write odd = odd.next but in even = even.next.next?

  • @viveksoni6823
    @viveksoni6823 8 месяцев назад

    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;

  • @neerajmalhotra8277
    @neerajmalhotra8277 27 дней назад

    What software you are using for drawing?

  • @adityalingwal6029
    @adityalingwal6029 8 месяцев назад

    Lovely Explanation

  • @stevie_wanders
    @stevie_wanders 2 месяца назад

    What is the digital whiteboard that you use?

  • @LAVLESHSINGH-s1r
    @LAVLESHSINGH-s1r 7 месяцев назад

    actually there is prblem oocuring after writing evehead=head.next still it can t connect it to the even with last index of odd

  • @MultiFacebookers
    @MultiFacebookers 6 месяцев назад

    Why do we need evenhead? Why can’t we make last odd point to head.next which will logically be first even position?

  • @santhoshl-zq1op
    @santhoshl-zq1op Месяц назад

    thanks for this video

  • @Mighty_kaaju
    @Mighty_kaaju 11 месяцев назад

    great explanation

  • @YourCodeVerse
    @YourCodeVerse Год назад

    Understood✅🔥🔥

  • @arnabkundu1648
    @arnabkundu1648 7 месяцев назад

    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.

  • @hardikpatel352
    @hardikpatel352 8 месяцев назад

    Thanks a lot striver, understood

  • @rushidesai2836
    @rushidesai2836 9 месяцев назад

    Time complexity O(n/2) because the while loop will run max of n/2 times.

  • @samarthpai5359
    @samarthpai5359 Месяц назад +1

    The time complexity of the optimal solution is O(N/2) and space complexity is O(1)
    Edit: it was O(N)

  • @vidhiarora4995
    @vidhiarora4995 5 месяцев назад

    hey striver....what if the last node is an odd one?
    how will it justify that the odd is dependent on even?

    • @arjunk04
      @arjunk04 4 месяца назад

      In this case even going to be null which is taken care by the condition specified in while loop

  • @s_savarnaya
    @s_savarnaya 7 месяцев назад

    i have a doubt the odd = odd->next->next instead of odd=odd->next

  • @toushikbanik8452
    @toushikbanik8452 6 месяцев назад

    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.

  • @zainiqbal7990
    @zainiqbal7990 8 месяцев назад

    What sincerity Striver. Respect Man

  • @yashendrabadal4776
    @yashendrabadal4776 2 месяца назад

    Peak Content !

  • @akshathlm7849
    @akshathlm7849 11 месяцев назад +1

    Time Complexity O(N/2)

  • @harshitkhandelwal2661
    @harshitkhandelwal2661 7 месяцев назад

    We can also include another case if its only 2 element Linked List then also we do nothing

  • @saravanansubbiah164
    @saravanansubbiah164 9 месяцев назад

    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.

  • @aronkumar-v8i
    @aronkumar-v8i 10 месяцев назад

    time complexity O 2(n/2)
    space O(1)

  • @shubhamrathod9249
    @shubhamrathod9249 Месяц назад

    thank you

  • @nishigandhagirishdandekar1131
    @nishigandhagirishdandekar1131 5 месяцев назад

    I think it will be O(n) because we are traversing all the elements for even and odd.

  • @mhdvlogs2130
    @mhdvlogs2130 3 месяца назад

    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

  • @sunayanabhowmik9690
    @sunayanabhowmik9690 6 месяцев назад

    Also, without edge case we get runtime error: if(head == null || head.next == null) return head;

  • @BhavyaJain-qz8jg
    @BhavyaJain-qz8jg 6 месяцев назад +2

    solved without watching, kudos to previous videos base creation !!

  • @NazeerBashaShaik
    @NazeerBashaShaik 9 месяцев назад

    Understood, thank you.

  • @TON-108
    @TON-108 Год назад

    How can we do this using two while loops, one for even and one for odd?? I'm getting Runtime error!!

    • @pravinthakur7791
      @pravinthakur7791 10 месяцев назад

      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;
      }

  • @shaikkhizar8133
    @shaikkhizar8133 10 месяцев назад +1

    Time Complexity=O(N/2)
    Space Complexity=O(1)
    Thankyou for your Lecture

  • @Harsh-jc2bz
    @Harsh-jc2bz 5 месяцев назад

    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;
    }
    }

  • @nishant4595
    @nishant4595 4 месяца назад

    was able to solve by optimal meself!

  • @DhanushS-we6ql
    @DhanushS-we6ql 3 месяца назад

    small coreetcion while even node is pointing to
    even = even->next;

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 9 месяцев назад

    Thanks A lot Bhaiya

  • @hulkstar0808
    @hulkstar0808 7 месяцев назад

    Understood , Thank youu

  • @Aditya_6996
    @Aditya_6996 9 месяцев назад

    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 9 месяцев назад

      the link would already been updated. so odd->next would give you the third node instead of 2nd node

    • @Aditya_6996
      @Aditya_6996 9 месяцев назад

      ​@@vipinms4864 yeah understood that already but thanks

  • @rohanlambar4725
    @rohanlambar4725 10 месяцев назад

    Time Complexity - O(n/2)

  • @ANNAM-dv5dy
    @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 ...?

    • @pavanilakshmiaishwarya8962
      @pavanilakshmiaishwarya8962 10 месяцев назад

      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

  • @SaketK11
    @SaketK11 Год назад +1

    Thanx bro

  • @fitnessforlifetamil3936
    @fitnessforlifetamil3936 10 месяцев назад

    Understood 🎉

  • @3idiots-iiitdmjabalpur
    @3idiots-iiitdmjabalpur Год назад

    understood 👍

  • @ManishLakkavatri
    @ManishLakkavatri Год назад +1

    Understood, but one small doubt ,why didn't we consider the operations in the while loop as unit operations?

    • @shreyxnsh.14
      @shreyxnsh.14 Год назад

      unit =1 and they are two operations

    • @shreyxnsh.14
      @shreyxnsh.14 Год назад

      also you can see that you are traversing through every single element.

  • @42owaishansari90
    @42owaishansari90 11 месяцев назад

    Understood Sir

  • @mainakdasgupta7130
    @mainakdasgupta7130 16 дней назад

    thank you man

  • @square-kstudios9561
    @square-kstudios9561 Год назад

    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).

  • @khalasianiket816
    @khalasianiket816 6 месяцев назад

    understood❤

  • @moksh7130
    @moksh7130 3 месяца назад

    Understood sir! :D

  • @ramkrishnapatil2105
    @ramkrishnapatil2105 4 месяца назад

    understood😄

  • @DeadPoolx1712
    @DeadPoolx1712 4 месяца назад

    UNDERSTOOD;

  • @rumiNITPatna
    @rumiNITPatna Месяц назад

    thank u sir

  • @deadlockgaming8553
    @deadlockgaming8553 9 месяцев назад

    what if its an odd length linked list?

  • @nikhilkumarjamwal5322
    @nikhilkumarjamwal5322 Год назад

    Nice one! :)

  • @SurajKumar-ku1lg
    @SurajKumar-ku1lg 9 месяцев назад

    he knows where everyone make mistakes like o(n/2)

  • @jogeshsoni2205
    @jogeshsoni2205 8 месяцев назад

    in pseudo code of optimal approach , it should be even=even.next instead of even=even.next.next in while loop.