Giudoku Official Site
Another method for reversing a list

Note: this algorithm is strongly based on Java functionalities I was forced to learn and use for an exercise, and that let me plan a new technique.
It's very easy to reverse an array using recursion: every step replaces the element of index I with the element of index N-I (where N is the size of the array) and the base case occurs when the algorithm arrives to the half of the array. The same applies to any data structure where you can operate on an element given its index.

The exercise required a method for reversing a list that takes as only parameter a ListIterator object connected to the list.
What my code does is removing every element from the list until it's empty, and then when there's no next element, a chain of returns starts, and the objects are put back in the list in reverse order. You can see this if you run the code, thanks to the messages.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;

public class ReverseList
{
  public static void main(String[] args)
  {
    List<Integer> l = new LinkedList<Integer>();
    int[] values = {10, 20, 5, 6, 8};
    
    for (int i = 0; i < values.length; i++)
    {
      l.add(values[i]);
    }
    
    System.out.println("List before rearranging: " + l);
    ListIterator<Integer> it = l.listIterator();
    reverse(it);
    System.out.println("List after rearranging: " + l);
  }
  
  public static void reverse(ListIterator<Integer> it)
  {
    Object ob;
    
    if (it.hasNext())
    {
      ob = it.next();
      it.remove();
      System.out.println("Removing " + ob);
      
      reverse(it);
      
      it.add((Integer)ob);
      System.out.println("Adding " + ob + " again");
    }
  }
}