Single Linked List (Deleting the Last Node)

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

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

  • @nikhilbantu3583
    @nikhilbantu3583 4 ปีที่แล้ว +132

    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 9 หลายเดือนก่อน +3

      exactly

    • @omjeepragya4113
      @omjeepragya4113 7 หลายเดือนก่อน

      amazing '

    • @gowthamkrishna4745
      @gowthamkrishna4745 6 หลายเดือนก่อน

      Bro we can write the code without function call

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

      true

    • @Sbakade2
      @Sbakade2 4 หลายเดือนก่อน +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 8 หลายเดือนก่อน +15

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

  • @sudhanwapande6857
    @sudhanwapande6857 4 ปีที่แล้ว +58

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

    • @therealpunit
      @therealpunit 4 หลายเดือนก่อน +1

      Adha Paid Hai

  • @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 7 หลายเดือนก่อน

      now how will you free ptr that you created.

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

      ​@@apoorvanupam8154by the code free(ptr) 😅

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

    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 ปีที่แล้ว +75

    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 9 หลายเดือนก่อน +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

  • @tiffinsuresh860
    @tiffinsuresh860 3 ปีที่แล้ว +8

    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 10 หลายเดือนก่อน

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

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

  • @usamakhan-yy2ee
    @usamakhan-yy2ee 4 ปีที่แล้ว +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)

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

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

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

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

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

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

  • @HarshKumar-ec8lv
    @HarshKumar-ec8lv 4 ปีที่แล้ว +7

    Can u provide a python programming course also?

  • @gowthamkrishna4745
    @gowthamkrishna4745 6 หลายเดือนก่อน +2

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

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

  • @abhishekV007
    @abhishekV007 10 วันที่ผ่านมา

    Thank you soo much sir for this awesome presentation ❤

  • @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 🌸👏

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

    Pointer to pointer will be needed

  • @Codevibe001
    @Codevibe001 8 หลายเดือนก่อน

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

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

    The best teacher

  • @Pk-sw8ip
    @Pk-sw8ip 5 หลายเดือนก่อน

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

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

    You are awesome ...waiting for.more lectures

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

    I love ur work

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

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

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

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

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

  • @jithsunghsaivallabhaneni4170
    @jithsunghsaivallabhaneni4170 ปีที่แล้ว

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

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

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

    Thanku sir ....good presentation...

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

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

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

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

    Totally Understood! Thanks

  • @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 2 หลายเดือนก่อน

      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.

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

  • @AJAYSINGH-bc4ge
    @AJAYSINGH-bc4ge 3 ปีที่แล้ว

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

  • @mohamadzaki6151
    @mohamadzaki6151 4 หลายเดือนก่อน

    Thanks

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

  • @FaisalKhan-nl3rg
    @FaisalKhan-nl3rg ปีที่แล้ว

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

  • @shubhamdas6519
    @shubhamdas6519 ปีที่แล้ว

    thanks a lot ...sir...

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

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

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

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

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

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

    Please do datastructures in c++ also...

  • @DarshanHM-ts7gp
    @DarshanHM-ts7gp 3 หลายเดือนก่อน

    Yes we can you single pointer

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

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

  • @aslcakmak4592
    @aslcakmak4592 18 วันที่ผ่านมา

    What if we dont return the head and use head and temp pointers without creating the third pointer ? Does that count? We dont actually free the temp2 so we can use head instead of temp2 just in del_last function , i think.

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

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

  • @HelpMe-e3x
    @HelpMe-e3x 9 หลายเดือนก่อน

    Can u pliz add in the declarations as well?

  • @100mm_kalliber6
    @100mm_kalliber6 ปีที่แล้ว

    Why we are returning head over here on 3:53

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

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

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

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

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

    Yes we can❤

  • @Number-um9tc
    @Number-um9tc 3 ปีที่แล้ว

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

  • @l.y.h5016
    @l.y.h5016 7 หลายเดือนก่อน

    best bro

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

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

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

    Yes

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

    Sir why we use tp

  • @yesnamay
    @yesnamay ปีที่แล้ว

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

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

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

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

    Circular linked list sir please

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

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

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

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

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

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

    Pls upload java lectures daily..

  • @khushalnikam3561
    @khushalnikam3561 2 ปีที่แล้ว +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

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

  • @naeemezgg6457
    @naeemezgg6457 4 หลายเดือนก่อน

    I wish it was in python

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

  • @ohm.3768
    @ohm.3768 ปีที่แล้ว

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

  • @VishalYadav-ss4qv
    @VishalYadav-ss4qv 4 ปีที่แล้ว

    ❤️❤️❤️🙏🙏

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

    Oppppppppppp bolte

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

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

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

    #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 9 หลายเดือนก่อน

    Can u pliz add the declarations as well?