• 06-07-2003
Loki
Hey first of all I would like to say thanks for the help or even just reading this. Now to the problem if you have a singularly linked Link list in C and you wish to reverse the values which it holds can you?

Move the memory locations which the pointers next value points to thus saving the values ever coming out of memory and increasign efficency 10 fold. Its kinda along the same lines as bit shifting for multiplication I suppose ? I had this question posed to me in a mock interview and I went along the above described lines to my solution only to be told that this is impossible / wrong and so coming last :(

Any help much appreciated.

Thanks

Tom
• 06-07-2003
ygfperson
You probably should have explained in more detail. Computers have a very small view of things at the moment. It may seem easy to just move the pointers, but the technical work behind that is much more difficult. Imagine this:
Code:

`swap (A,B);`
That may look easy to you, a person with two hands. But the computer must allocate some extra space, C. The end result looks like this:
Code:

```C = A; A = B; B = C;```
Think about how to approach linking singly-linked lists backwards. Starting off at the first element, you follow its pointer to the next element. In order to change its direction, you need a seperate pointer to store the old information. Otherwise, you've effectively isolated those two elements from the rest of the linked list.

On the other hand...
Quote:

Move the memory locations which the pointers next value points to
Moving memory locations is impossible. Moving the data stored at those locations is inefficient. What you want to do is to change the pointer's value to the node behind it instead of the one in front of it.
• 06-07-2003
Prelude
The question really just asks you to swap the next pointers so that this:

0->1->2->3->4->5->6->7->8->9

becomes

9->8->7->6->5->4->3->2->1->0

Here's one way to do it:
Code:

```List reverseList(List list) {     Element newList = 0;     Element item = list;     Element forward;         while (item)     {         forward = item->next;         item->next = newList;         newList = item;         item = forward;     }         return newList; }```