Single Linked List (Deleting the Last Node)

preview_player
Показать описание
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.

Music:
Axol x Alex Skrindo - You [NCS Release]

#DataStructuresByNeso #DataStructures #LinkedList #SingleLinkedList
Рекомендации по теме
Комментарии
Автор

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 .

nikhilbantu
Автор

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

beyinvekalp
Автор

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

sudhanwapande
Автор

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;

}
}

AnimeManiaa
Автор

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*

anuvindm
Автор

// 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

swornimshah
Автор

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

shivp
Автор

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

atulmishra
Автор

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.

tiffinsuresh
Автор

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)

coders
Автор

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;

ganeshreddykomitireddy
Автор

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

csworld
Автор

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!!😊

maca
Автор

I did with this method "struct node *DeleteLast_Node(struct node* head){
struct node *temp = head;

temp = temp->link;
}
free(temp->link);
temp->link = NULL;
return head;
}"

Pk-swip
Автор

Your teaching is very good and easily undest

KavyaPotnuru
Автор

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

Codevibe
Автор

Can u provide a python programming course also?

HarshKumar-eclv
Автор

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)

usamakhan-yyee
Автор

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

daverussell
Автор

You are awesome ...waiting for.more lectures

souradipkumarsaha