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 .
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
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; } }
@@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.
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*
@@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.
// 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
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.
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;
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)
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!!😊
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)
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
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
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.
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.
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
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.
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; }
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 ...
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
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?????
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
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 .
exactly
amazing '
Bro we can write the code without function call
true
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
can't imagine computer engineering without @neso Academy. Thank you 🥰🥰🥰
This course is really good.. Appreciate you for providing this premium course in free
Adha Paid Hai
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;
}
}
as you are working with only 3 nodes
Yeah but thats harder to read
@@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.
now how will you free ptr that you created.
@@apoorvanupam8154by the code free(ptr) 😅
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*
Thanks for sharing
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.
@@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.
@@tenacity4839 O(n)
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;
}
Is it correct ? @nesoacademy
U forgot to do
ptr->link->link = NULL;
at the end.
@@ishmam8643 no, just
ptr->link = NULL
@@MrJzvoyeur Yeah, There was no need to do ptr->link->link = NULL
Nice
// 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
Can u provide a python programming course also?
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.
we have to return head only in else -if case , not in "if" and "else" case
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;
You should also free the nth node
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)
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!!😊
Your teaching is very good and easily undest
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 . 👍
can we do this by using length of the link .if yes? which way is faster
I like your afford and hardworking.... Please keep continue
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)
Pointer to pointer will be needed
Thank you so much for these lectures ,These are too helpful 😊
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;
}"
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;
}
very helpful 🌸👏
Let's say temp is at (n-1)th node then...
Free(temp->link);
temp-> link=NULL;
DOES IT WORK?
Can u pliz add in the declarations as well?
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.
we have to return head if there is only one node right? and for other two cases there is no need.
The best teacher
Thanks
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
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
Yes we can you single pointer
temp(= second last node)
free(temp->link);
temp-> link=NULL;
temp =NULL;
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;
}
}
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.
Can u upload a series on Power electronics ?
Best course free thank u so much sir
You are awesome ...waiting for.more lectures
4:49 . yes we can delete last node using a single pointer
Yes sir.. using of temp->link-> link !=NULL
I love ur work
Please do datastructures in c++ also...
NOTE:we can write the code without function call..
For deleting using only one pointer, you can traverse till temp->link->link != NULL. and rest is the same logic, all in one traversal.
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.
Absolutely wrong to move the head pointer
Sir why we use tp
Totally Understood! Thanks
No need of temp 2 pointer ,we can use head->link->link = Null
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.
thank you 8/04/2022 at 1:20 am
Thanku sir ....good presentation...
Deletion using only one pointer:
void delete_end(struct node *head)
{
if(head == NULL)
coutnext;
ptr->next = NULL;
free(ptr->next);
}
}
thanks a lot ...sir...
while calling del_last function by main function instead of del_last you wrote del_first . plz correct it.
while running the program in compiler it was that data was not declared ;
what to do?
Dear Neso Academy , Please reply 🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼🙏🏼. what is the difference between datastructures and advanced datastructures?
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
Yes sir, we use single pointer to delete last node of list
Yes we can❤
we will traverse till end and check if(ptr==NULL){
free(ptr);
ptr = NULL; }
Yes
sir or simply use free(ptr->next);
Program is giving segmentation fault ...someone pls help me out with this
Ans: I think no coz we have to free the space that is reserved by the last node.
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.
@@anshumaan1024
yes, see next video
Program giving segmentation fault .... someone help
best bro
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;
}
Circular linked list sir please
Why we are returning head over here on 3:53
Sir will you please post the link for the every program used in the video because getting error while coding.
void delLast(node *head){
if(head==NULL){
coutlink->link!=NULL){
temp=temp->link;
}
free(temp->link);
temp->link=NULL;
}
}
it is c++ code
I wish it was in python
Pls upload java lectures daily..
Program not running segmentation fault aa raha hai
Ye half program hi hae..
Did u create head pointers usinv malloc ? Cz ye program mae ye sab nahi hae..
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 ...
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
@@filibertorios5889 yup that's correct.. Thank you
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;
}
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?????
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
What you thought was right
The code is god damn big unable to recall it and little difficult to understand
Oppppppppppp bolte
❤️❤️❤️🙏🙏
Amazed by your teaching sir! Many many thanks to you🙏🤍
#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
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
Can u pliz add the declarations as well?