Single Linked List (Deleting the Last Node)

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

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

  • @nikhilbantu3583
    @nikhilbantu3583 4 года назад +129

    After creating all nodes ,let's take ptr=head and while(ptr->link->link !=null) in this loop we will write ptr=ptr->link.after the loop ptr points the node before the last node we want to delete .we will free ptr->link and make ptr->link =null .

    • @onakrieth5614
      @onakrieth5614 8 месяцев назад +3

      exactly

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

      amazing '

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

      Bro we can write the code without function call

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

      true

    • @Sbakade2
      @Sbakade2 3 месяца назад +1

      Who knows link list has only three node, it can be either one node or no link itself, this logic works only in case of three node or more than three node

  • @beyinvekalp
    @beyinvekalp 7 месяцев назад +13

    can't imagine computer engineering without @neso Academy. Thank you 🥰🥰🥰

  • @sudhanwapande6857
    @sudhanwapande6857 4 года назад +56

    This course is really good.. Appreciate you for providing this premium course in free

  • @AnimeManiaa
    @AnimeManiaa 3 года назад +32

    Instead of using temp2 , we can use temp->link->link != NULL in the while loop to delete last node using single pointer
    void del(struct point* head)
    {
    struct point *ptr=head;
    while(ptr->link->link !=NULL)
    {
    ptr=ptr->link;
    }
    free(ptr->link);
    ptr->link = NULL;
    }
    }

    • @HemantKrMeena-ln1jk
      @HemantKrMeena-ln1jk 2 года назад

      as you are working with only 3 nodes

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

      Yeah but thats harder to read

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

      @@Chilli_Tea Absolutely. what if you have 100 nodes in the list. Super hard to read. The concept of using two pointers (struct node *temp_cur) and (struct node *temp_prev) makes it easier to understand and control.

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

      now how will you free ptr that you created.

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

      ​@@apoorvanupam8154by the code free(ptr) 😅

  • @anuvindm2092
    @anuvindm2092 2 года назад +22

    Yes, we can delete the last node with only one pointer except head.
    First we will traverse the list and once ptr->link is NULL we have reached the last node and we can free it by free(ptr).
    _Now note that we need a variable 'count 'in the traversal which is incremented each time through the while loop (initially count=1). Now, when the traversal is over we will get a number count (here, count will be 3 as there are 3 nodes).
    Next part is to traverse the list again within a loop which iterates < count times (here, < 3 times. i.e, 2 times). So we will get the 2nd node and we can assign ptr -> link = NULL
    *This might not get in your head because of the way I am explaining, but this way works and this is a simple way for sure and I am just explaining my method, you guys can have your own way*

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

      Thanks for sharing

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

      This is a very good explanation and I agree with your thought. Correct me if I am wrong, your program will take O(n^2) which is not that efficient.

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

      @@tenacity4839 Actually it's O(n). It would have been O(n²) if one loop is inside the other loop. In this case, it's consecutive loops so O(n) should be the time complexity.

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

      @@tenacity4839 O(n)

  • @shivp436
    @shivp436 4 года назад +74

    Solution with only one pointer:
    void delete_end(struct node *head)
    {
    struct node *ptr = head;
    if(head == NULL) printf("List is already empty");
    else if(head->link == NULL) {
    free(head);
    head = NULL;
    }
    else {
    while(ptr->link->link != NULL) {
    ptr = ptr->link;
    }
    free(ptr->link);
    ptr-> NULL;
    }

    • @shivp436
      @shivp436 4 года назад +1

      Is it correct ? @nesoacademy

    • @ishmam8643
      @ishmam8643 4 года назад +6

      U forgot to do
      ptr->link->link = NULL;
      at the end.

    • @MrJzvoyeur
      @MrJzvoyeur 4 года назад +12

      @@ishmam8643 no, just
      ptr->link = NULL

    • @ishmam8643
      @ishmam8643 4 года назад +1

      @@MrJzvoyeur Yeah, There was no need to do ptr->link->link = NULL

    • @mr.miniactor3818
      @mr.miniactor3818 4 года назад

      Nice

  • @swornimshah8898
    @swornimshah8898 8 месяцев назад +1

    // simple step is to use link->link approach, this you we get the second last node in a linked list
    while(temp->link !=NULL && temp->link->link != NULL)
    temp = temp->link;
    // temp = second last node
    // the reason for checking temp->link != NULL is to avoid null pointer crashes i.e NULL->link crashes program

  • @HarshKumar-ec8lv
    @HarshKumar-ec8lv 4 года назад +7

    Can u provide a python programming course also?

  • @tiffinsuresh860
    @tiffinsuresh860 3 года назад +7

    we do have to update head. In case if there's only one node we free the node and make head = NULL which means head will get updated in one of the conditions. So we have to return head as it gets updated.

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

      we have to return head only in else -if case , not in "if" and "else" case

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

    We can do delete the last node by traversing through the list and finding the length of the list(count).And then intialize n=0
    ,Ptr=head and then
    while(n!=count-1)
    {
    n++;
    Ptr=Ptr->link;
    }
    Now Ptr is pointing to the n-1 th node.
    Ptr->link=NULL;

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

      You should also free the nth node

  • @coders8180
    @coders8180 3 года назад +2

    Yes..we can do it with single pointer except head pointer...1st we assign head->link->link=null and 3rd pointer is on 3rd elements and we'll free this 3rd pointer by assigned null to this(supposed to be 3 elements in our LL)

  • @maca4137
    @maca4137 4 года назад +10

    I have one doubt sir, are you going to upload array topic videos in data structures or array videos in C programming playlists are enough. BTW thanks for uploading regularly sir!!😊

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

    Your teaching is very good and easily undest

  • @atulmishra2593
    @atulmishra2593 4 года назад +6

    Thank you sir for posting such a good content . And providing a free education . Your way of explaining is really very good .
    Keep it up sir . 👍

  • @vivektangudu4547
    @vivektangudu4547 3 года назад +2

    can we do this by using length of the link .if yes? which way is faster

  • @csworld9319
    @csworld9319 4 года назад +2

    I like your afford and hardworking.... Please keep continue

  • @usamakhan-yy2ee
    @usamakhan-yy2ee 3 года назад +4

    1. Insert a Node at the start of a linked list. (Cover all edge cases)
    2. Insert a Node at the end of a linked list. (Cover all edge cases)
    3. Remove a Node at the end of a linked list (Cover all edge cases)

  • @manoharsinghrawat1859
    @manoharsinghrawat1859 4 года назад +1

    Pointer to pointer will be needed

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

    Thank you so much for these lectures ,These are too helpful 😊

  • @Pk-sw8ip
    @Pk-sw8ip 4 месяца назад

    I did with this method "struct node *DeleteLast_Node(struct node* head){
    struct node *temp = head;
    while(temp->link->link!=NULL){
    temp = temp->link;
    }
    free(temp->link);
    temp->link = NULL;
    return head;
    }"

  • @accessdenied9393
    @accessdenied9393 3 года назад

    The correct code that uses one pointer: (checked with all possible cases)
    void delete_last(node *head)
    {
    node *tmp = head;
    if(!head){
    puts("List is already empty"); return;}
    else if(!head->ptr){
    puts("The list contains one node only"); return;}
    else
    {
    while(tmp->ptr->ptr) tmp = tmp->ptr;
    free(tmp->ptr);
    tmp->ptr = NULL;
    }
    tmp = NULL;
    return;
    }

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

    very helpful 🌸👏

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

    Let's say temp is at (n-1)th node then...
    Free(temp->link);
    temp-> link=NULL;
    DOES IT WORK?

  • @HelpMe-e3x
    @HelpMe-e3x 8 месяцев назад

    Can u pliz add in the declarations as well?

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

    void del_last(struct abc *head){
    struct abc *pt = NULL;
    pt = head;
    while(pt -> ptr -> ptr != NULL){
    pt = pt -> ptr;
    }
    free(pt -> ptr -> ptr);
    pt -> ptr = NULL;
    }
    struct abc is the name of the datatype and contains self-referential structure *ptr.

  • @abhishekgadewar4616
    @abhishekgadewar4616 3 года назад

    we have to return head if there is only one node right? and for other two cases there is no need.

  • @Codality
    @Codality 3 года назад

    The best teacher

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

    Thanks

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

    Suppose if there is only one node in the list, then we will free that node and assign head to NULL in the del_last function but this will not affected in main function if you don't return head pointer

  • @daverussell4052
    @daverussell4052 3 года назад

    and if you working with current like the last lecture where you insert at the end using current you should update the current also to the temp2 because if you delete the last node the current will be point to unknown memory allocation

  • @DarshanHM-ts7gp
    @DarshanHM-ts7gp 2 месяца назад

    Yes we can you single pointer

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

    temp(= second last node)
    free(temp->link);
    temp-> link=NULL;
    temp =NULL;

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

    void del_last(struct node *head){
    if(head == NULL){
    printf("LinkedList is empty");
    }
    else if(head->link == NULL){
    free(head);
    head = NULL;
    }
    else{
    struct node *temp = head;
    while(temp->link->link != NULL){
    temp = temp->link;
    }
    free(temp->link->link);
    temp->link = NULL;
    }
    }

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

      Using free(temp->link->link); is incorrect because it tries to free a NULL pointer. You should use free(temp->link); to correctly free the last node itself.

  • @ShivamKumarBEE
    @ShivamKumarBEE 4 года назад +1

    Can u upload a series on Power electronics ?

  • @HarshKumar-ec8lv
    @HarshKumar-ec8lv 4 года назад

    Best course free thank u so much sir

  • @souradipkumarsaha9267
    @souradipkumarsaha9267 4 года назад +1

    You are awesome ...waiting for.more lectures

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

    4:49 . yes we can delete last node using a single pointer

  • @AJAYSINGH-bc4ge
    @AJAYSINGH-bc4ge 3 года назад

    Yes sir.. using of temp->link-> link !=NULL

  • @betrp
    @betrp 4 года назад +2

    I love ur work

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

    Please do datastructures in c++ also...

  • @gowthamkrishna4745
    @gowthamkrishna4745 5 месяцев назад +1

    NOTE:we can write the code without function call..

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

    For deleting using only one pointer, you can traverse till temp->link->link != NULL. and rest is the same logic, all in one traversal.

  • @pushkaranand7141
    @pushkaranand7141 4 года назад +1

    We can use head pointer along with temp pointer. And then we wont return the head so it wont be reflected in the main function.

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

      Absolutely wrong to move the head pointer

  • @mmahaboobbasha9438
    @mmahaboobbasha9438 3 года назад

    Sir why we use tp

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

    Totally Understood! Thanks

  • @FaisalKhan-nl3rg
    @FaisalKhan-nl3rg Год назад

    No need of temp 2 pointer ,we can use head->link->link = Null

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

    Last question answes.
    Yes we can use only one temp pointer and insted of giving address of second last node to 2nd pointer, just give it to head and proceed.

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

    thank you 8/04/2022 at 1:20 am

  • @divyavv9109
    @divyavv9109 4 года назад

    Thanku sir ....good presentation...

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

    Deletion using only one pointer:
    void delete_end(struct node *head)
    {
    if(head == NULL)
    coutnext;
    ptr->next = NULL;
    free(ptr->next);
    }
    }

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

    thanks a lot ...sir...

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

    while calling del_last function by main function instead of del_last you wrote del_first . plz correct it.

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

    while running the program in compiler it was that data was not declared ;
    what to do?

  • @monicabattacharya6416
    @monicabattacharya6416 3 года назад +1

    Dear Neso Academy , Please reply 🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼. what is the difference between datastructures and advanced datastructures?

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

    void deletelastnode(struct node *head){
    while(head->link->link!=NULL){
    head = head->link;
    }
    struct node *temp;
    temp = head->link;
    head->link = NULL;
    free(temp);
    temp = NULL;
    }
    this is for if number of node is more than 1....
    and for general you can apply condition using if else ladder

  • @Number-um9tc
    @Number-um9tc 3 года назад

    Yes sir, we use single pointer to delete last node of list

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

    Yes we can❤

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

    we will traverse till end and check if(ptr==NULL){
    free(ptr);
    ptr = NULL; }

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

    Yes

  • @kalppariya381
    @kalppariya381 4 года назад

    sir or simply use free(ptr->next);

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

    Program is giving segmentation fault ...someone pls help me out with this

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

    Ans: I think no coz we have to free the space that is reserved by the last node.

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

      i think we can free the last node also by using free(temp->next) and then make temp->next=NULL.
      Because the second last node will also contain the last node address.

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

      @@anshumaan1024
      yes, see next video

  • @narendrajatav9969
    @narendrajatav9969 3 года назад

    Program giving segmentation fault .... someone help

  • @l.y.h5016
    @l.y.h5016 6 месяцев назад

    best bro

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

    We can use single pointer (ptr) to delete the last element of the list using below logic:
    -------------------------------------------------------------------------------------------------------------------------------------------
    void Delete(struct node *ptr)
    {
    while(pt->link->link != NULL)
    {
    ptr = ptr->link;
    }
    free(ptr->link);
    ptr->link=NULL;
    }

  • @rochak2608
    @rochak2608 4 года назад

    Circular linked list sir please

  • @100mm_kalliber6
    @100mm_kalliber6 Год назад

    Why we are returning head over here on 3:53

  • @MaheshSharma-gj4rg
    @MaheshSharma-gj4rg 4 года назад

    Sir will you please post the link for the every program used in the video because getting error while coding.

  • @nitishdiwakar8490
    @nitishdiwakar8490 3 года назад

    void delLast(node *head){
    if(head==NULL){
    coutlink->link!=NULL){
    temp=temp->link;
    }
    free(temp->link);
    temp->link=NULL;
    }
    }

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

    I wish it was in python

  • @chandubadam4395
    @chandubadam4395 4 года назад

    Pls upload java lectures daily..

  • @dailyfunde1805
    @dailyfunde1805 3 года назад

    Program not running segmentation fault aa raha hai

    • @anveshatagore542
      @anveshatagore542 3 года назад

      Ye half program hi hae..
      Did u create head pointers usinv malloc ? Cz ye program mae ye sab nahi hae..

  • @aman33247
    @aman33247 4 года назад

    No I think is answer , it's not possible to do it with only 1 temp variable , because if we do so... We'll lose the link of the last node , and hence the last node cannot be deleted ...

    • @filibertorios5889
      @filibertorios5889 4 года назад +1

      i was able to come up with a solution with only using one temp variable:
      struct node *temp = head;
      while(temp->next->next != NULL){ //traverses the given list up to the second to last node, so temp is pointing to 1 node before the last node
      temp = temp->next;
      }
      free(temp->next); //passes address to the last node and frees up the last node from heap, leaving temp pointing to what is now the last node
      temp->next = NULL; //makes sure there are no invalid pointers making temp the last node
      this is code for the `else` block of the if construct, the `if` and `else if` blocks are the same

    • @aman33247
      @aman33247 4 года назад

      @@filibertorios5889 yup that's correct.. Thank you

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

    I think so answer of that que. would be as follows:-
    #include
    #include
    struct Node
    {
    int data;
    struct Node *link;
    };
    struct Node *delete_end(struct Node *p,struct Node *h,int count)
    {
    p = h;
    struct Node *temp;
    for (int i=1;ilink;
    }
    }
    free(temp->link);
    temp->link = NULL;
    }
    int main()
    {
    struct Node *head,*pointer,*newnode;
    head = NULL;
    int choice = 1,count = 0;
    while (choice == 1)
    {
    newnode = malloc(sizeof(struct Node));
    printf("Enter the No:
    ");
    scanf("%d",&newnode->data);
    newnode->link = NULL;
    if (head == NULL)
    {
    head = pointer = newnode;
    }
    else
    {
    pointer->link = newnode;
    pointer = newnode;
    }
    count+=1;
    printf("Enter 1 if u want to continue:
    ");
    scanf("%d",&choice);
    }

    pointer = head;
    while(pointer!=NULL)
    {
    printf("%d ",pointer->data);
    pointer = pointer->link;
    }
    printf("
    ");

    delete_end(pointer,head,count);
    pointer = head;
    while(pointer!=NULL)
    {
    printf("%d ",pointer->data);
    pointer = pointer->link;
    }
    return 0;
    }

  • @sravanthkorada2939
    @sravanthkorada2939 3 года назад

    Here it's mentioned that there is no need of updating the head pointer by returning it from the function. But, if the linked list contains only one node, then after deleting it we need to return a NULL pointer and update the list else it will print that data. Isn't it?????

    • @KothaabhinayReddy
      @KothaabhinayReddy 3 года назад +1

      If we want to delete the first node we should return the head but in the case taken in the video we are not deleting the head node so no need of returning the head

    • @KothaabhinayReddy
      @KothaabhinayReddy 3 года назад

      What you thought was right

  • @ohm.3768
    @ohm.3768 Год назад

    The code is god damn big unable to recall it and little difficult to understand

  • @adityalochan7256
    @adityalochan7256 3 года назад

    Oppppppppppp bolte

  • @VishalYadav-ss4qv
    @VishalYadav-ss4qv 4 года назад

    ❤️❤️❤️🙏🙏

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

    Amazed by your teaching sir! Many many thanks to you🙏🤍

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

    #include
    #include
    struct node{
    int data;
    struct node*link;
    };
    struct node* del_last(struct node*head){
    struct node*temp1,*temp2=head;
    while(temp1->link!=NULL){
    temp2=temp1;
    temp1=temp1->link;
    }
    temp2->link=NULL;
    free(temp1);
    temp1=NULL;
    return head;
    }
    int main(){
    struct node*head=malloc(sizeof(struct node));
    head->data=54;
    head->link=NULL;
    struct node*current=malloc(sizeof(struct node));
    current->data=85;
    current->link=NULL;
    head->link=current;
    current=malloc(sizeof(struct node));
    current->data=71;
    current->link=NULL;
    head->link->link=current;
    while(head!=NULL){
    printf("%d\t",head->data);
    head=head->link;
    }
    head=del_last(head);
    while(head!=NULL){
    printf("%d\t",head->data);
    head=head->link;
    }
    }
    The program is giving segmentation fault ..pls someone help me out

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

    Yes
    #include
    using namespace std;
    class node{
    public:
    int data;
    node *link;
    };
    int main(){
    coutdata = 4;
    second->link = third;
    third->data = 8;
    third->link = NULL;
    // head = head->link;
    node *p = NULL;
    p=head;
    while(p->link != NULL){
    coutdata link;
    }
    p = NULL;
    node *p1 = NULL;
    p1=head;
    cout

  • @HelpMe-e3x
    @HelpMe-e3x 8 месяцев назад

    Can u pliz add the declarations as well?