First time here? Checkout the FAQ!
+2 votes


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

asked in DS by Veteran (56.7k points)   | 77 views

2 Answers

+4 votes
Best answer

1st iteration:


$2$nd iteration:

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

answered by Veteran (51k points)  
selected by
good one :)
+3 votes
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
answered by Veteran (15.1k points)  

Related questions

0 votes
1 answer
asked in DS by iarnav Active (2.5k points)   | 77 views
0 votes
2 answers
asked in DS by smartmeet Loyal (3.3k points)   | 424 views
0 votes
1 answer
asked in DS by smartmeet Loyal (3.3k points)   | 63 views

Top Users Aug 2017
  1. Bikram

    5388 Points


    4730 Points

  3. manu00x

    3582 Points

  4. akash.dinkar12

    3534 Points

  5. rahul sharma 5

    3196 Points

  6. makhdoom ghaya

    2710 Points

  7. just_bhavana

    2432 Points

  8. stblue

    2244 Points

  9. Tesla!

    2126 Points

  10. pawan kumarln

    1914 Points

25,076 questions
32,240 answers
30,249 users