In the code below, i passed the head pointer by value. Can you tell me how does it get modified?

node* reverseList(node *head){

node *prev=NULL;

node *curr=head;

node *Next=NULL;

while(curr!=NULL){

Next=curr->next;

curr->next=prev;

prev=curr;

curr=Next;

}

head=prev;

return head;

}

int main()
{

node *head=NULL;

cin>>head;

node *p=reverseList(head)

cout<<head;

cout<<endl<<“Reverse:”<<endl;

cout<<p;

/*<< and >> operators are overloaded */

}

In the above code if I insert the list as 1,2,3 and 4.

I get the output as :

1

Reverse

4

3

2

1

hello @iamlovelesh
here we r dealing with pointers,when u r passing root node that it is correct that it is passed as value.
but its value i,e address will remain same .
so whatever changes u will do at that value.
it will be reflected in list.

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.