Insertion Sort List - Leetcode 147 - Python

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

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

  • @john_youtube
    @john_youtube 2 года назад +64

    Just got my job at Amazon and I can say without a doubt you're a major reason for that. Thank you neetcode

  • @danielshvartz9702
    @danielshvartz9702 11 месяцев назад +3

    worth to mention
    that prev pointer will always point to the last sorted element
    so when we do prev.next = curr.next
    we do it to save the next nodes and to continue from the last sorted node.

  • @zabona
    @zabona Год назад +7

    tmp = dummy, or you will not sort first element. Dummy is not even used after it's initialized.

  • @ssenthilnathan3
    @ssenthilnathan3 2 года назад +7

    I just learned insertion sort, and you posted this problem. Very Thankyou !!

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

    Even if you don't do the extra comparison with the last element, this algorithm is still O(n) best case, but this best case happens when the initial linked list is sorted in reverse order.

  • @orellavie6233
    @orellavie6233 2 года назад +2

    Why not checking if temp.next is not none before actually using its .val?

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

      Because cur.val is guaranteed to be smaller than prev.val, tmp will first reach prev before it reaches None

  • @chendong2197
    @chendong2197 2 года назад

    after initializing the dummy node, which point to head. When assigning head to prev with (prev = head). Are you assigning the dummy node to prev or the old head to prev? 9th line

    • @cosepeter2197
      @cosepeter2197 2 года назад

      old head is assigned to prev not the dummy node. This done because it's not required to check the first node , so cur node start from the second node

  • @christiangualteros36
    @christiangualteros36 2 года назад +1

    hello. i have a question silly but i don't know how I can proof this example and what I need to call for print in the screem the result. thank you

    • @anmolbakshi7983
      @anmolbakshi7983 2 года назад

      start iterating from the head until it becomes null and print each node's value

    • @christiangualteros36
      @christiangualteros36 2 года назад +1

      @@anmolbakshi7983 thank you. You're a great person

    • @venkateshvardhineedi9571
      @venkateshvardhineedi9571 2 года назад

      ListNode temp = head
      while(temp != null){
      System.out.println(temp.val)
      temp = temp.next
      }

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

    Shukriya !

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

    Beautiful explanation

  • @AlexSav
    @AlexSav 2 года назад +1

    11:05 Is't it crash on empty list? Line 9 (head might be None)

  • @alaaal-habashna4722
    @alaaal-habashna4722 2 года назад

    Thank you! This is very clear.

  • @nathnaeldereje5127
    @nathnaeldereje5127 2 года назад

    Great Job, That was so helpful.

  • @카이트-c8y
    @카이트-c8y Год назад +1

    Why this code gives TLE?
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode() {}
    * ListNode(int val) { this.val = val; }
    * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    * }
    */
    class Solution {
    public ListNode insertionSortList(ListNode head) {
    ListNode helpHead = new ListNode(-9999, head); //without set pointer dummy.next is works
    ListNode curr = head;
    while (curr != null) {
    ListNode prev = helpHead;
    while(prev.next != null && prev.next.val

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

    Hello, why my code in java is not correct(Time Limit Exceeded)?
    ListNode dummy = head;
    ListNode curr = head.next;
    ListNode prev = head;
    ListNode tmp = null;
    while( curr != null){
    if (curr.val >= prev.val){
    prev = curr;
    curr = curr.next;
    continue;
    }
    tmp = head;
    while(curr.val > tmp.next.val){
    tmp = tmp.next;
    }
    prev.next = curr.next;
    curr.next = tmp.next;
    tmp.next = curr;
    curr = prev;
    }
    return dummy.next;

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

      That's most likely because two first elements are not sorted, and because of a mistake in the video you keep inserting second element before itself. Try changing tmp initialization to tmp = dummy

  • @Notaarav6876
    @Notaarav6876 2 года назад

    Hi it’s me again I am your biggest fan plz read this comment u r helping me so much

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

    Thank You

  • @erkhembayarbayart3966
    @erkhembayarbayart3966 2 года назад

    nice explanation! thank u

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

    you are the best!!!

  • @meme-blower
    @meme-blower Год назад

    super helpful

  • @shidharthraj9310
    @shidharthraj9310 2 года назад

    Really helpful 😀👍

  • @samirpaul2
    @samirpaul2 2 года назад +1

    👍👍❤❤⭐⭐🙌🙌

  • @masternobody1896
    @masternobody1896 2 года назад +1

    how do I get a job at fang. what do I need to learn. its been 2 years i have been unemployed