Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This problem is not much different from any other reversal problem. One solution is to walk the list, and place each data item into an array. Then, walk the list again, but start at the end of the array, and you overwrite the list data with the values from the array in reverse order.</p> <pre><code>for (n in list) arr[i++] = n-&gt;data for (n in list) n-&gt;data = arr[--i] </code></pre> <p>If you are not allowed to store anything, then recursion is also out, since it acts as an auxiliary array to store your reversed list. Then a silly solution would be to implement a random access interface to the list:</p> <pre><code>Node * nth_list_item (Node *list, int n); </code></pre> <p>Which returns the <code>n</code><sup>th</sup> item of the list. Then, you reverse the list with this interface that lets access it like an array (with a time penalty of course). The reversal no longer takes O(n) time, but is now O(n<sup>2</sup>).</p> <hr> <p>If recursion is okay, then to satisfy the spirit of the "without storing elements" requirement, you need to walk the list recursively, and then reverse the list as you unwind. This can be accomplished by allowing another parameter to the recursive call to provide the pointer needed to walk through the beginning the list as the recursive calls are unwinding from the end. This implementation uses no extra local variables per recursive call, only the variables provided in the parameters.</p> <pre><code>void swap_left (node *a, node *b, int tmp) { a-&gt;data = b-&gt;data; b-&gt;data = tmp; } void reverse_recursively (node *cur, node **tail) { if (cur) { reverse_recursively(cur-&gt;next, tail); if (*tail) { swap_left(cur, *tail, cur-&gt;data); if (cur != *tail) *tail = (*tail)-&gt;next; if (cur == *tail) *tail = 0; } } } void reverse (node *list) { reverse_recursively(list, &amp;list); } </code></pre> <hr> <p>If we are allowed to encroach upon the spirit of the "without storing elements" requirement when using recursion, then there is a more straight forward (but more space hungry) solution. Basically, a copy of the reversed linked list can be created while recursively traversing it. When the end of it is reached, the list can be traversed again, copying in the elements from the reversed copy.</p> <pre><code>#define make_node(n, d) (node){ n, d } void reverse_recursively (node *list, node *cur, node copy) { if (!cur) { for (cur = &amp;copy; cur; cur = cur-&gt;next, list = list-&gt;next) { list-&gt;data = cur-&gt;data; } return; } reverse_recursively(list, cur-&gt;next, make_node(&amp;copy, cur-&gt;data)); } void reverse (node *list) { if (list == 0) return; reverse_recursively(list, list-&gt;next, make_node(0, list-&gt;data)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload