Reverse the linked list

My code is working fine but I have one query. IN the below code if I will print the head before reverse operation then it will print all the elements but If I will print the head after the reverse operation head is only printing a single element while reverse LL is printing all the elements

CODE: https://ide.codingblocks.com/s/601560

@rahul_bansal after reversing, the node that “head” is holding has become the last element of the list hence only 1 element is printed.

But I am using a different pointer for reversing the LL then why the head is changing?

@rahul_bansal head2 and head1 are pointers to the same node, you did not make a copy of the node. After you reverse the list, head2 ends up pointing to the new head (which was earlier the last node). But head is still pointing to that node only, and that node is now at the end of that list.

Oh okay I understand so can u please edit the code so that head will print the LL actually I am stucking in this.

@rahul_bansal head2 is printing the complete LL

@rahul_bansal if you want head to print the LL, dont use head2. reverse(head) and print(head)

I want to make two LL. the head is the given LL and head2 is reverse LL. I am not want to modify head thatswhy I use head2.

@rahul_bansal okay i’ll modify your code accordingly

@rahul_bansal you are modifying the original list in your function, so you willl need to create a copy of the list if you dont want the original list to be changed

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.