65 views

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I cannot understand how this portion of code working for reverse. Can somebody explain?

asked in DS | 65 views

1st iteration:

$2$nd iteration:

After one interation : edge between $Z$ and $X$ is reversed.

selected by
good one :)
we have taken 3 pointers as
X, Y and Z.

i. X is pointing to Head
ii. Y pointing to the next node of X
initially Z is Null hence,

X->next = Z, will make head node last node

Z will point to the node pointed by X, and now X and Will point to the second node.

in next iteration
Y will point to 3rd node, 2nd node's pointer will point to the first node which was head earlier.
and so on..

PS: don't consider this part of code only, but consider the entire while loop