LeetCode Reorder List Solution Explained - Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ธ.ค. 2024
  • The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
    Join my free exclusive community built to empower programmers! - www.skool.com/...
    Preparing For Your Coding Interviews? Use These Resources
    --------------------
    (My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
    AlgoCademy - algocademy.com...
    Daily Coding Interview Questions - bit.ly/3xw1Sqz
    10% Off Of The Best Web Hosting! - hostinger.com/...
    Follow Me on X/Twitter - x.com/nickwhit...
    Follow My Instagram - / nickwwhite
    Other Social Media
    ----------------------------------------------
    Discord - / discord
    Twitch - / nickwhitettv
    TikTok - / nickwhitetiktok
    LinkedIn - / nicholas-w-white
    Show Support
    ------------------------------------------------------------------------------
    Patreon - / nick_white
    PayPal - paypal.me/nick....
    Become A Member - / @nickwhite
    #coding #programming #softwareengineering

ความคิดเห็น •

  • @miadinh6381
    @miadinh6381 2 ปีที่แล้ว +4

    Strategy starts from 3:09

  • @yachnagupta2081
    @yachnagupta2081 5 ปีที่แล้ว +9

    How about traversing LinkedList till middle using slow pointer (jumps one) and fast pointer(jumps two)....Then from middle put the nodes in the stack... Start traversing LinkedList from head keep poping elements from stack and insert at a gap of one node in LinkedList.

    • @HarshaKalluri
      @HarshaKalluri 5 ปีที่แล้ว +15

      thats extra space.

    • @safiabegum7060
      @safiabegum7060 5 หลายเดือนก่อน

      Can u be more clear

  • @rifathossain2440
    @rifathossain2440 4 ปีที่แล้ว +24

    My mans always looking high :)

  • @shamanthhegde2786
    @shamanthhegde2786 4 ปีที่แล้ว +3

    can you say how the merging condition breaks in odd number of linked list.

  • @jollysrivastava3326
    @jollysrivastava3326 4 ปีที่แล้ว +16

    Hi Nick, you may need to rework on your logic for the merge. It's not working fine for odd length array.

    • @ommule4999
      @ommule4999 3 ปีที่แล้ว +4

      This logic works for odd & even length LinkedLists ! No issues

    • @jacksonripper-mp8dr
      @jacksonripper-mp8dr 9 หลายเดือนก่อน

      and you need to rework on developing your brain.....

    • @jollysrivastava3326
      @jollysrivastava3326 9 หลายเดือนก่อน

      @@jacksonripper-mp8dr I did. Thanks for letting me know. I just forgot to use my brain.

    • @jacksonripper-mp8dr
      @jacksonripper-mp8dr 9 หลายเดือนก่อน

      @@jollysrivastava3326 I was just kidding.... Everyone learns from mistakes.

  • @vasurohilla5812
    @vasurohilla5812 11 หลายเดือนก่อน +1

    where can i found the source code

  • @nik15oc
    @nik15oc 4 ปีที่แล้ว +5

    Whats the time and space complexities?
    Time Complexity: O(n) & Space Complexity: O(1) ?

    • @abe10
      @abe10 4 ปีที่แล้ว +1

      Yeah, I guess. We traverse all the nodes twice and constant space for temporary variables.

  • @vikramreddy7586
    @vikramreddy7586 4 ปีที่แล้ว +3

    My dogs are barking in the background :D Like always an awesome explanation. !!

  • @antoniomartinez3429
    @antoniomartinez3429 4 ปีที่แล้ว +2

    what about traversing the list and pushing values into an array first?

    • @rajarshibose5122
      @rajarshibose5122 4 ปีที่แล้ว +7

      That is something you are not allowed to do for most interviews.Else you can solve every problem like that.....Do that only when you have no other way possible ...

    • @ianpan0102
      @ianpan0102 4 ปีที่แล้ว

      @@rajarshibose5122 agreed

  • @samwilson4597
    @samwilson4597 2 ปีที่แล้ว

    I first tried it with a stack. this solution is a bit tricky

  • @akshitgupta7665
    @akshitgupta7665 5 ปีที่แล้ว +2

    brilliant

  • @DheerajSharma-fs6je
    @DheerajSharma-fs6je ปีที่แล้ว

    Thank You!!

  • @raginibhadani7257
    @raginibhadani7257 ปีที่แล้ว +1

    merge logic does not work correctly

  • @honey-xr5kp
    @honey-xr5kp 8 หลายเดือนก่อน

    doesnt work for me

  • @aishwarygupta19
    @aishwarygupta19 9 หลายเดือนก่อน

    Does anyone else getting a "Time Limit Exceeded" error

  • @prathamanand1037
    @prathamanand1037 3 ปีที่แล้ว

    thanks

  • @safiabegum7060
    @safiabegum7060 5 หลายเดือนก่อน

    Explain merge function again
    Mergin both lists is difficult

  • @ianpan0102
    @ianpan0102 4 ปีที่แล้ว +1

    My C++ solution using the same idea:
    """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
    class Solution {
    public:
    void reorderList(ListNode* head) {
    if (!head || !head->next || !head->next->next) return;
    ListNode *slow = head, *fast = head, *head1 = head, *head2, *tmp1, *tmp2;
    while (fast && fast->next) {
    slow = slow->next;
    fast = fast->next->next;
    }
    head2 = slow->next;
    slow->next = nullptr; // split list in half
    head2 = reverseList(head2); // reverse 2nd half
    while (head1 && head2) {
    tmp1 = head1->next;
    head1->next = head2;
    tmp2 = head2->next;
    head2->next = tmp1;
    head1 = tmp1;
    head2 = tmp2;
    }
    }
    private:
    ListNode* reverseList(ListNode* head) {
    ListNode* prev = nullptr;
    ListNode* curr = head;
    ListNode* tmp;
    while (curr) {
    tmp = curr->next;
    curr->next = prev;
    prev = curr;
    curr = tmp;
    }
    return prev;
    }
    };

  • @jagdishwarbiradar1763
    @jagdishwarbiradar1763 4 ปีที่แล้ว +3

    fast and slow doesn't work properly

  • @ramesh_hegde
    @ramesh_hegde 2 หลายเดือนก่อน

    nice dog bark in background

  • @viditsharma3929
    @viditsharma3929 3 ปีที่แล้ว

    great.

  • @Adarsh-mn7pl
    @Adarsh-mn7pl 2 ปีที่แล้ว +1

    Not working for this case
    Input
    [1,2,3,4,5]
    stdout
    1 2
    5 4 3
    Output
    [1,5,2,4]
    Expected
    [1,5,2,4,3]

  • @johncho9160
    @johncho9160 11 หลายเดือนก่อน

    the way youre explaining the merge method is confusing af and the way youre naming these variables certainly dont help

  • @millenialmusings8451
    @millenialmusings8451 2 ปีที่แล้ว +1

    man you spend a lot of time on the intro talking unnecessary trivia.. the actual video starts at 3:09.. plz get straight to the point

    • @wirelessbrain12
      @wirelessbrain12 2 ปีที่แล้ว +2

      No you just don't know what he's talking about before that. He's going through what your mindset should be when breaking down a problem. For ex he talks about 2 pointers because that's the solution for arrays and hence you would think of that to see if it's applicable but then explains why it's not.