Query regarding passing of variables in the function

for this question , bhaiya saved the code at

i have one problem in this,
while studying linked list we were told that if we want the changes to be made to our actual linked list we have to pass the node pointer to a function by reference that is as
datatype function(node*&head)

but in this question the data is passed by value that as
int replacesum(node* root)

even doing this the code is working perfectly as if it is passed by reference.

kindly tell what is happening here???

@Parmeet-Kalsi-1631789033630118,
func(node* root) already means you are passing a reference of the root.
You need to pass by reference when you are not passing pointers, eg passing a variable or container.
Eg func(int x) vs func(int &x) and func(vector<int> v) vs func(vector<int> &v).

i don’t understand , why is it not same as that in linked list
there in linked list we were having a pointer to our object that is head and to pass it by reference we used (node*&head),
but here in tree we are also having a pointer to our object and here to pass it by reference we are using (node*root)

why???

please reply fast!!!

@Parmeet-Kalsi-1631789033630118,
Node* root in linked list should work fine as well.

So it means that you are saying that node*head in linked list means we are passing by reference

But bhaiya specially told in a video that node * head in linked list means passing by value and to pass by reference we have to use node *& head.

Please clear this doubt , it is getting extended a lot although it is a simple yes/no doubt.

@Parmeet-Kalsi-1631789033630118,
Okay, see this code snippet,

class Node {
public:
	int data;
	Node* nxt;

	Node(int x = 0) {
		data = x;
		nxt = NULL;
	}
};

void push_front(Node* &head, int x) {
	Node* temp = new Node(x);
	temp->nxt = head;
	head = temp;
}

void increment(Node* head) {
	while (head) {
		head->data++;
		head = head->nxt;
	}
}

void print(Node* head) {
	while (head) {
		cout << head->data << ", ";
		head = head->nxt;
	}
}

In this the push_front requires Node* &head because we permanently update the value of the head pointer declared in main, but the print function does not require a reference, because we do not update any pointer. Also, in increment we do some permanent changes on our linked list, but still we don’t need to pass a reference pointer, because we only make changes to the linked list, not the head pointer we have passed.
The head pointer remains same before and after the increment and print function calls, thus we don’t need to pass it by reference.
The head pointer itself changes (the list has a new head) after calling push_front, therefore we need to pass by reference.
Similarly, the replaceSum function in bhaiya’s implementation, does not change the actual head of the linked list, thus does not requires passing by reference.

Also, I am sure bhaiya emphasised on that point wrt his implementation, because otherwise generalising the statement that implementing linked list would indeed mean passing pointers by reference wouldn’t be very accurate as I can make a class like this and refrain myself from passing any sort of pointers.

class List {
	class Node {
	public:
		int data;
		Node* nxt;

		Node(int x = 0) {
			data = x;
			nxt = NULL;
		}
	};

	Node* head;

public:
	List() {
		head = NULL;
	}

	void push_front(int x) {
		Node* temp = new Node(x);
		temp->nxt = head;
		head = temp;
	}

	void increment() {
		Node* temp = head;
		while (temp) {
			temp->data++;
			temp = temp->nxt;
		}
	}

	void print() {
		Node* temp = head;
		while (temp) {
			cout << temp->data << ", ";
			temp = temp->nxt;
		}
	}
};

Great , i got it now…