GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
68 views

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

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

asked in DS by Veteran (52.5k points)   | 68 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 (43.9k 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.6k points)  

Related questions

0 votes
2 answers
1
asked in DS by smartmeet Loyal (3.2k points)   | 80 views
0 votes
0 answers
2
asked in DS by smartmeet Loyal (3.2k points)   | 31 views


Top Users Mar 2017
  1. rude

    5246 Points

  2. sh!va

    3054 Points

  3. Rahul Jain25

    2920 Points

  4. Kapil

    2732 Points

  5. Debashish Deka

    2602 Points

  6. 2018

    1574 Points

  7. Bikram

    1444 Points

  8. Vignesh Sekar

    1440 Points

  9. Akriti sood

    1424 Points

  10. Sanjay Sharma

    1128 Points

Monthly Topper: Rs. 500 gift card

21,556 questions
26,908 answers
61,270 comments
23,278 users