Insert a node in a Singly Linked List at a given position (Implementation)

Поділитися
Вставка
  • Опубліковано 26 січ 2025

КОМЕНТАРІ • 57

  • @itsdineshvaryani
    @itsdineshvaryani  4 роки тому +1

    Please *Subscribe* and *Click Bell* 🔔🔔🔔 Icon for More Updates. To get *Data Structures and Algorithms* complete course for free please follow this link - ua-cam.com/play/PL6Zs6LgrJj3tDXv8a_elC6eT_4R5gfX4d.html

  • @shubhamagarwal1434
    @shubhamagarwal1434 2 роки тому +3

    Best series for DS & Algo. I am also 10 yrs java exp guy. Was looking for DS & Algo free course over UA-cam with java implementation and found this. Hats Off To You Man...Excellent Work. GOD BLESS YOU :)

  • @FashionTales1
    @FashionTales1 Рік тому +3

    I see many people learning DSA with C/C++, but I decided to learn it in JAVA for full stack development related opportunities. But the resources are very few for DSA in JAVA😶 Luckily I found this🤞. Thank you so much sir for giving some hope for many people like me...! Lastly, Thanks for spreading this Knowledge instead of selling it🤩👏👏👏. I will definitely share this with many👍
    - Samanvitha : )

  • @hobojoesnr.3283
    @hobojoesnr.3283 3 роки тому +2

    at 5:47 , couldnt you use a for loop instead of a while loop at line 57?
    for(int count = 1; count < position - 1; count++){}

  • @kashifsiddiqui4042
    @kashifsiddiqui4042 2 роки тому +1

    Very well explained! Thank you so much for making DSA this much easy!!

  • @dhruvipaliwal8961
    @dhruvipaliwal8961 2 роки тому +2

    You made dsa easy to understand

  • @nirmesh44
    @nirmesh44 4 роки тому +2

    i am watching all your videos. very nicely explained👌

    • @itsdineshvaryani
      @itsdineshvaryani  4 роки тому

      Thanks !!! Please share it with ur friends and colleagues. This will help channel grow and motivate me to add more and more videos.

  • @dineshr4732
    @dineshr4732 3 роки тому +1

    Thanks a lot Dinesh Varyani. This has helped me a lot.

  • @raz0229
    @raz0229 3 роки тому +3

    Never knew light IDE themes could be so catchy :D

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому +1

      Yes it is !!!

    • @raz0229
      @raz0229 3 роки тому +3

      @@itsdineshvaryani You sir, are a legend for still being able to reply to all the comments on your channel. Much love

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому +1

      @@raz0229 No problem ... Its a part of youtube journey !!!

    • @Chill_dood_007
      @Chill_dood_007 3 роки тому +1

      Dinesh Kindly say how could i change the theme of eclipse like the one in this video

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому +2

      Thats not eclipse .... Its intellij idea ...

  • @GemTechnocrat
    @GemTechnocrat Місяць тому +1

    This will fail in case position specified is larger than the current list size. If list has 4 element and I asked to insert an element at position 8 then it will fail. Corner case needs to be considered

  • @manojmani5452
    @manojmani5452 2 роки тому +2

    Nice explanation sir

  • @poojithakumaran8946
    @poojithakumaran8946 2 роки тому +1

    @Dinesh Varyani why cant u discuss the time and space complexity for each operations insert,delete etc...

  • @sambeetrath7328
    @sambeetrath7328 3 роки тому +1

    What if the given position is invalid? Should we write an if statement at the beginning of insert ()?

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому +1

      You can provide that edge case ... But its assumed position is valid !!!

    • @karentechnologies3990
      @karentechnologies3990 2 роки тому +4

      You can make the following changes in your code if you want to check if a given position is valid or not
      private void insertAtPosition(int value, int position, SinglyLinkedList singlyLinkedList){
      ListNode node = new ListNode(value);
      if (position == 0 || position > singlyLinkedList.length()+1){
      System.out.println("Error, Invalid Position");
      return;
      }
      }

  • @MelodyWorldddzz
    @MelodyWorldddzz 2 роки тому +1

    You are amazing sir ❤️

  • @abhishekpatel6020
    @abhishekpatel6020 Рік тому

    What will happen if we add at certain position like 5 when list is empty

    • @golcuk2076
      @golcuk2076 7 місяців тому

      nullpointer exception. Because target 'next' will be empty at one position.

  • @mr.kdrama2.o53
    @mr.kdrama2.o53 2 роки тому

    sir notes or ppt dona interiview keliye read karna hai apke easy hai notes plz

  • @raviteja3121
    @raviteja3121 Місяць тому

    Super 🎉

  • @pushkarkumaryadav2570
    @pushkarkumaryadav2570 3 роки тому +1

    Sir Thank you very much for this but why there is so less likes ?

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому +2

      you share it with ur friends and colleagues on linkedin and facebook as post and you will see magic !!!

  • @chinomsojohnson8746
    @chinomsojohnson8746 3 роки тому

    I just started learning DSA and I am curious on why your count starts from one and not zero. Any particular reason why?

    • @itsdineshvaryani
      @itsdineshvaryani  3 роки тому

      you can start from 0 ... it doesnt matter ... just make calculations based on 0

    • @chinomsojohnson8746
      @chinomsojohnson8746 3 роки тому

      @@itsdineshvaryani thanks for responding

    • @neelchatterjee.1368
      @neelchatterjee.1368 2 роки тому

      @@chinomsojohnson8746 then while runs till (position-2)

  • @afaqkhan2243
    @afaqkhan2243 3 роки тому +1

    Very nice sir

  • @nishantpatill
    @nishantpatill 3 роки тому +1

    public void insert(int position,int data){

    ListNode newNode=new ListNode(data);
    if(position==1){
    newNode.next=head;
    head=newNode;
    }
    else{
    ListNode previous=head;
    int count = 1;
    while(count < position-1);{
    previous=previous.next;
    count++;
    }
    ListNode current=previous.next;
    previous.next=newNode;
    newNode.next=current;

    }
    }
    Exception in thread "main" java.lang.NullPointerException: Cannot read field "next" because "previous" is null
    at SinglyLinkedList.insert(SinglyLinkedList.java:40)

    • @karentechnologies3990
      @karentechnologies3990 2 роки тому

      You can make the following changes in your code if you want to check if a given position is valid or not
      private void insertAtPosition(int value, int position, SinglyLinkedList singlyLinkedList){
      ListNode node = new ListNode(value);
      if (position == 0 || position > singlyLinkedList.length()+1){
      System.out.println("Error, Invalid Position");
      return;
      }
      }

  • @achiever27
    @achiever27 3 роки тому

    Amazing DSA playlist

  • @karentechnologies3990
    @karentechnologies3990 2 роки тому +2

    You can make the following changes in your code if you want to check if a given position is valid or not
    private void insertAtPosition(int value, int position, SinglyLinkedList singlyLinkedList){
    ListNode node = new ListNode(value);
    if (position == 0 || position > singlyLinkedList.length()+1){
    System.out.println("Error, Invalid Position");
    return;
    }
    }

  • @sanjushekhawat6446
    @sanjushekhawat6446 4 роки тому +1

    Thanks sir