Single Linked List (Deleting the Last Node)

  Рет қаралды 176,068

Neso Academy

Neso Academy

Күн бұрын

Data Structures: Deleting the Last Node of a Singly Linked List
Topics discussed:
1) C Program for deleting the last node of single linked lists.
C Programming Lectures: goo.gl/7Eh2SS
Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
Contribute: www.nesoacademy...
Memberships: bit.ly/2U7YSPI
Books: www.nesoacademy...
Website ► www.nesoacademy...
Forum ► forum.nesoacade...
Facebook ► goo.gl/Nt0PmB
Twitter ► / nesoacademy
Music:
Axol x Alex Skrindo - You [NCS Release]
#DataStructuresByNeso #DataStructures #LinkedList #SingleLinkedList

Пікірлер: 145
@beyinvekalp
@beyinvekalp 9 ай бұрын
can't imagine computer engineering without @neso Academy. Thank you 🥰🥰🥰
@nikhilbantu3583
@nikhilbantu3583 4 жыл бұрын
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 10 ай бұрын
exactly
@omjeepragya4113
@omjeepragya4113 9 ай бұрын
amazing '
@gowthamkrishna4745
@gowthamkrishna4745 8 ай бұрын
Bro we can write the code without function call
@mzafarullah4670
@mzafarullah4670 7 ай бұрын
true
@Sbakade2
@Sbakade2 6 ай бұрын
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
@sudhanwapande6857
@sudhanwapande6857 4 жыл бұрын
This course is really good.. Appreciate you for providing this premium course in free
@therealpunit
@therealpunit 5 ай бұрын
Adha Paid Hai
@AnimeManiaa
@AnimeManiaa 3 жыл бұрын
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 3 жыл бұрын
as you are working with only 3 nodes
@Chilli_Tea
@Chilli_Tea Жыл бұрын
Yeah but thats harder to read
@fc_miles
@fc_miles Жыл бұрын
@@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 9 ай бұрын
now how will you free ptr that you created.
@common1237
@common1237 3 ай бұрын
​@@apoorvanupam8154by the code free(ptr) 😅
@shivp436
@shivp436 4 жыл бұрын
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 жыл бұрын
Is it correct ? @nesoacademy
@ishmam8643
@ishmam8643 4 жыл бұрын
U forgot to do ptr->link->link = NULL; at the end.
@MrJzvoyeur
@MrJzvoyeur 4 жыл бұрын
@@ishmam8643 no, just ptr->link = NULL
@ishmam8643
@ishmam8643 4 жыл бұрын
@@MrJzvoyeur Yeah, There was no need to do ptr->link->link = NULL
@mr.miniactor3818
@mr.miniactor3818 4 жыл бұрын
Nice
@atulmishra2593
@atulmishra2593 4 жыл бұрын
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 . 👍
@anuvindm2092
@anuvindm2092 3 жыл бұрын
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 жыл бұрын
Thanks for sharing
@tenacity4839
@tenacity4839 2 жыл бұрын
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 2 жыл бұрын
@@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)
@tiffinsuresh860
@tiffinsuresh860 3 жыл бұрын
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 11 ай бұрын
we have to return head only in else -if case , not in "if" and "else" case
@coders8180
@coders8180 4 жыл бұрын
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 жыл бұрын
I like your afford and hardworking.... Please keep continue
@swornimshah8898
@swornimshah8898 11 ай бұрын
// 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
@SUPERBILLA
@SUPERBILLA Ай бұрын
We can also traverse like this While ( temp->link->link !=NULL) { temp=temp->link; }
@sivagamer532
@sivagamer532 Ай бұрын
Bhai agar two node hi rahega to Ye loop nahi chalega bro 🤔🤔
@sivagamer532
@sivagamer532 Ай бұрын
Aage phir aapka marji bro 😊😊
@maca4137
@maca4137 4 жыл бұрын
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!!😊
@_txt_7398
@_txt_7398 Жыл бұрын
very helpful 🌸👏
@ganeshreddykomitireddy5128
@ganeshreddykomitireddy5128 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
@Codevibe001
@Codevibe001 10 ай бұрын
Thank you so much for these lectures ,These are too helpful 😊
@abhishekV007
@abhishekV007 2 ай бұрын
Thank you soo much sir for this awesome presentation ❤
@KavyaPotnuru
@KavyaPotnuru 8 ай бұрын
Your teaching is very good and easily undest
@HarshKumar-ec8lv
@HarshKumar-ec8lv 4 жыл бұрын
Can u provide a python programming course also?
@mohammedajmal4264
@mohammedajmal4264 2 жыл бұрын
4:49 . yes we can delete last node using a single pointer
@usamakhan-yy2ee
@usamakhan-yy2ee 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 жыл бұрын
Pointer to pointer will be needed
@Codality
@Codality 3 жыл бұрын
The best teacher
@vivektangudu4547
@vivektangudu4547 3 жыл бұрын
can we do this by using length of the link .if yes? which way is faster
@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
@betrp
@betrp 4 жыл бұрын
I love ur work
@souradipkumarsaha9267
@souradipkumarsaha9267 4 жыл бұрын
You are awesome ...waiting for.more lectures
@Sri.k_Codes
@Sri.k_Codes 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
@aslcakmak4592
@aslcakmak4592 2 ай бұрын
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.
@HarshKumar-ec8lv
@HarshKumar-ec8lv 4 жыл бұрын
Best course free thank u so much sir
@austin2508
@austin2508 2 жыл бұрын
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.
@zaid3586
@zaid3586 3 жыл бұрын
Totally Understood! Thanks
@ShivamKumarBEE
@ShivamKumarBEE 4 жыл бұрын
Can u upload a series on Power electronics ?
@abhishekgadewar4616
@abhishekgadewar4616 3 жыл бұрын
we have to return head if there is only one node right? and for other two cases there is no need.
@divyavv9109
@divyavv9109 4 жыл бұрын
Thanku sir ....good presentation...
@gowthamkrishna4745
@gowthamkrishna4745 8 ай бұрын
NOTE:we can write the code without function call..
@Pk-sw8ip
@Pk-sw8ip 7 ай бұрын
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; }
@pushkaranand7141
@pushkaranand7141 4 жыл бұрын
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
@lokendrasinghrao
@lokendrasinghrao 3 жыл бұрын
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.
@AJAYSINGH-bc4ge
@AJAYSINGH-bc4ge 3 жыл бұрын
Yes sir.. using of temp->link-> link !=NULL
@areykesava
@areykesava 3 жыл бұрын
while running the program in compiler it was that data was not declared ; what to do?
@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.
@mohamadzaki6151
@mohamadzaki6151 5 ай бұрын
Thanks
@HelpMe-e3x
@HelpMe-e3x 11 ай бұрын
Can u pliz add in the declarations as well?
@kashishroopchandani
@kashishroopchandani 25 күн бұрын
Thank you so much
@FaisalKhan-nl3rg
@FaisalKhan-nl3rg Жыл бұрын
No need of temp 2 pointer ,we can use head->link->link = Null
@tenacity4839
@tenacity4839 2 жыл бұрын
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 3 ай бұрын
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.
@DarshanHM-ts7gp
@DarshanHM-ts7gp 5 ай бұрын
Yes we can you single pointer
@uniqueelsa5959
@uniqueelsa5959 3 жыл бұрын
Let's say temp is at (n-1)th node then... Free(temp->link); temp-> link=NULL; DOES IT WORK?
@shubhamdas6519
@shubhamdas6519 Жыл бұрын
thanks a lot ...sir...
@srilathapavan6549
@srilathapavan6549 2 жыл бұрын
Please do datastructures in c++ also...
@monicabattacharya6416
@monicabattacharya6416 3 жыл бұрын
Dear Neso Academy , Please reply 🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼. what is the difference between datastructures and advanced datastructures?
@ankurchaudhary924
@ankurchaudhary924 3 жыл бұрын
while calling del_last function by main function instead of del_last you wrote del_first . plz correct it.
@pnkdtu27
@pnkdtu27 11 ай бұрын
Yes we can❤
@mmahaboobbasha9438
@mmahaboobbasha9438 3 жыл бұрын
Sir why we use tp
@aisharyaroy3521
@aisharyaroy3521 2 жыл бұрын
Deletion using only one pointer: void delete_end(struct node *head) { if(head == NULL) coutnext; ptr->next = NULL; free(ptr->next); } }
@reverbism
@reverbism 2 жыл бұрын
thank you 8/04/2022 at 1:20 am
@shalukumari2199
@shalukumari2199 2 жыл бұрын
Program is giving segmentation fault ...someone pls help me out with this
@jithsunghsaivallabhaneni4170
@jithsunghsaivallabhaneni4170 Жыл бұрын
temp(= second last node) free(temp->link); temp-> link=NULL; temp =NULL;
@l.y.h5016
@l.y.h5016 8 ай бұрын
best bro
@Number-um9tc
@Number-um9tc 3 жыл бұрын
Yes sir, we use single pointer to delete last node of list
@karanchouhan2247
@karanchouhan2247 3 жыл бұрын
Yes
@CenturyCelebrators
@CenturyCelebrators 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
@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.
@kalppariya381
@kalppariya381 4 жыл бұрын
sir or simply use free(ptr->next);
@narendrajatav9969
@narendrajatav9969 3 жыл бұрын
Program giving segmentation fault .... someone help
@yesnamay
@yesnamay Жыл бұрын
we will traverse till end and check if(ptr==NULL){ free(ptr); ptr = NULL; }
@100mm_kalliber6
@100mm_kalliber6 Жыл бұрын
Why we are returning head over here on 3:53
@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; }
@reverbism
@reverbism 2 жыл бұрын
Ans: I think no coz we have to free the space that is reserved by the last node.
@anshumaan1024
@anshumaan1024 2 жыл бұрын
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 жыл бұрын
@@anshumaan1024 yes, see next video
@rochak2608
@rochak2608 4 жыл бұрын
Circular linked list sir please
@nitishdiwakar8490
@nitishdiwakar8490 4 жыл бұрын
void delLast(node *head){ if(head==NULL){ coutlink->link!=NULL){ temp=temp->link; } free(temp->link); temp->link=NULL; } }
@nitishdiwakar8490
@nitishdiwakar8490 4 жыл бұрын
it is c++ code
@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..
@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 жыл бұрын
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
@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 жыл бұрын
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
@chandubadam4395
@chandubadam4395 4 жыл бұрын
Pls upload java lectures daily..
@khushalnikam3561
@khushalnikam3561 2 жыл бұрын
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; }
@naeemezgg6457
@naeemezgg6457 6 ай бұрын
I wish it was in python
@VishalYadav-ss4qv
@VishalYadav-ss4qv 4 жыл бұрын
❤️❤️❤️🙏🙏
@adityalochan7256
@adityalochan7256 4 жыл бұрын
Oppppppppppp bolte
@Heellooo5052
@Heellooo5052 Жыл бұрын
Amazed by your teaching sir! Many many thanks to you🙏🤍
@SanjaykumarBN-rf3tj
@SanjaykumarBN-rf3tj 15 күн бұрын
void new_del_last(struct node* head){ struct node* temp = head; while(temp->link->link!=NULL){ temp->link = temp->link->link; } temp->link->link = NULL; free(temp->link); temp->link = NULL; }
@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 11 ай бұрын
Can u pliz add the declarations as well?
Single Linked List (Deleting the Last Node using Single Pointer)
4:01
Single Linked List (Deleting the Node at a Particular Position)
9:43
-5+3은 뭔가요? 📚 #shorts
0:19
5 분 Tricks
Рет қаралды 13 МЛН
Reverse a Single Linked List
11:57
Neso Academy
Рет қаралды 290 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 386 М.
Single Linked List (Deleting the First Node)
3:47
Neso Academy
Рет қаралды 199 М.
Single Linked List (Inserting a Node at a Certain Position)
6:52
Neso Academy
Рет қаралды 344 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 319 М.
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 992 М.
Deleting the Entire Single Linked List
6:06
Neso Academy
Рет қаралды 86 М.
Single Linked List (Inserting a Node at the Beginning)
5:37
Neso Academy
Рет қаралды 523 М.