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 (51.7k points)   | 65 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 (41.1k 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 (14.4k points)  

Related questions

0 votes
2 answers
asked in DS by smartmeet Loyal (3.1k points)   | 50 views
0 votes
0 answers
asked in DS by smartmeet Loyal (3.1k points)   | 27 views
Top Users Feb 2017
  1. Arjun

    5274 Points

  2. Bikram

    4230 Points

  3. Habibkhan

    3842 Points

  4. Aboveallplayer

    3086 Points

  5. Debashish Deka

    2378 Points

  6. sriv_shubham

    2308 Points

  7. Smriti012

    2236 Points

  8. Arnabi

    2008 Points

  9. sh!va

    1672 Points

  10. mcjoshi

    1640 Points

Monthly Topper: Rs. 500 gift card

20,845 questions
26,001 answers
22,093 users