Linked list Insert At head

Can you dry run this code??

hello @ashwani225
let say we have list
1->2->3->4
^
|
head

now we want to insert 5 to its head.
create node for 5.
5->NULL
now assign head to next of 5.
5->1->2->3->4
. . .^
. . .|
. .head

now update head to 5.
5->1->2->3->4
^
|
head

In this line node*n=new node(data); we have created node class object n and n->next=head; In this we are pointing n->next to head which is NULL then head=n; what this line for??

n->next=head;
this is to add the previously existing list to the next of n.

head=n (is to update the head)
as we have added new node to head of the link list so our new head will be n thats why
head=n;

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.