Linked list-insert at head

why head node is passed as a reference pointer, why just pointer is not doing the change, i think pointer does the same work but in the video he is passing pointer of refernce

@sktg99 If you don’t pass it by reference, any changes to it will only be available within the function call. For example, if it was not passed by reference, and you passed head (initialized to NULL ), to the insert() function, a new item would be created, but your value for head would only be updated inside the function.

but why is it happening, i have learnt in pointers that if a variable is passed using its pointer then its value also gets updated

I think you are confusing it with the concept of array. That happens when we pass an array, but when we pass a pointer variable then it’s local copy is made in the function call and as soon as the function ends that local copy is also destroyed.

can u give one example for that without using class?

im asking after creating a node pointer, that pointer stores the address of that node, and if we pass that address in function then all the changes will be made in that address so it should reflect changes but its not happening…

That happens in the case of an array or when a reference variable is passed. A pointer variable is different from a reference variable. In a function, a copy of the pointer variable is made, the changes in the copy bucket are not reflected in the original pointer variable. The address stored in the copy of a pointer variable is not changed, it is over-ridden.

can u send me any example code where pointer variable is passed?

You can refer to the code of the linkedlist. In that code a pointer variable is being passed only.

i want a example which is different from that node, i mean without using any class object, show me an example where int or char variable is used, im still confused in this concept

returnType funName(T varName) when a function is declared like this then a copy is created of any parameter which is passed to it. Here, T can be of any type, even a pointer as well, varName.
Unless an array is passed then this is the case of passing by value.
void func(int a) in this case variable a will be a copy of the parameter being passed to it.

returnType funName(T &varName) when a function is declared like this then a copy is not created rather the same variable which is being passed as a parameter, will be used in this function as well.
Here varName can be a pointer as well. void func(int &a) here variable is a reference variable and is not a copy of the value which is being passed to it as a parameter, rather, it will be the exact same value, and the changes made to this will be reflected in the original variable as well.

okay, can u please look my other 2 doubts, its urgent pls

I can’t see any doubts with your name, someone else must have acknowledged it.
And if one TA has acknowledged a doubt, others can’t take it.

yes someone has done it already