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 (49.9k points)   | 64 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 (37.6k 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 (11.7k points)  
Top Users Jan 2017
  1. Debashish Deka

    9716 Points

  2. sudsho

    5560 Points

  3. Bikram

    5290 Points

  4. Habibkhan

    4920 Points

  5. Vijay Thakur

    4498 Points

  6. Arjun

    4418 Points

  7. saurabh rai

    4236 Points

  8. Sushant Gokhale

    4230 Points

  9. Kapil

    3848 Points

  10. santhoshdevulapally

    3808 Points

Monthly Topper: Rs. 500 gift card

19,449 questions
24,228 answers
20,373 users