Question:
You have to reverse a linked list, but the condition is that data X which
resides in the node N should not change. For example,
Addr: 100 200 300 400 500
_____ _____ _____ _____ _____
| 1 | -->| 3 | --> | 5 | --> | 7 | -->| 9 | ---
----- ----- ----- ----- ----- |
----
--
should become
Addr: 100 200 300 400 500
_____ _____ _____ _____ _____
| 1 | <--| 3 | <-- | 5 | <-- | 7 | <--| 9 |
----- ----- ----- ----- -----
|
----
--
Answer:
Generally, we give the solution as follows:
Addr: 100 200 300 400 500
_____ _____ _____ _____ _____
| 9 | -->| 7 | --> | 5 | --> | 3 | -->| 1 | ---
----- ----- ----- ----- ----- |
----
--
We write a function reverse () which takes two arguments: one a pointer
to the head, and the other, pointer to second node of the list.
void
reverse ( node_t *hd, node_t *nxt )
{
if ( nxt && nxt -> next )
revesre ( nxt, nxt -> next );
nxt -> next = hd;
hd -> next = NULL;
}