ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to reverse a Java List: recursion, Multithreading, Permutations and parallel hardware

Updated on October 9, 2012

One of the annoying tests clients are increasingly using to screen candidates asked for ways to reverse a list. Since I have been trying to learn to think like an architect this gave me an excuse to play with different ways of reversing a list because the first way of doing something is rarely the best[1]. A quick Google search revealed some interest in this problem, so it may be useful to others. I used Java and the code should work with anything that implements the List interface, and some will work with any Collection, but the techniques used should work in any language, and may well be more concise and perhaps more efficient in (say) Python.

The coding itself is trivial, but the ideas behind the code get more complex as the article progresses. The progression is from simple reversal to threading to permutations and finally the role of massively parallel hardware is discussed.

The code samples here have not been optimised for performance, and have deliberately not been made generic. They assume a basic knowledge of Java and the ability to understand Java code.

Recursive and non recursive reversal

These two methods are simple. For recursion swap the two ends of the list, adjust the position of the ends and repeat on the remainder of the list. Stop when the two ends coincide or pass each other. This is an in-place reversal and needs minimal memory. The code should be self explanatory. Some micro optimisation may be possible at the cost of readability. The non recursive reversal is almost identical to the recursive version. Both take about the same time to run. The non recursive version is a little more intuitive.

Non recursive reversal

	public static void reverseList( List<String> theList, int start, int end)
    		int startpoint = start;
    		int endpoint = end;
         		String startString = theList.get(startpoint);
         		String endString  = theList.get(endpoint);
      	 		theList.set(endpoint, startString); 

Recursive reversal

public static void recursivereverse( List<String> theList, int start, int end)
        if(start>=end) return;
        String startpoint = theList.get(start);
        String endpoint = theList.get(end);
        theList.set(end, startpoint);

        recursivereverseCollection(theList, start, end);    

Stack Based Reversal

This is not an in-place reversal so it takes more memory. It was a lot less efficient and the execution time was very variable. This may mean that the time taken was long enough that the probability of a garbage collection sweep became high. It is elegant but not efficient.

public static Collection<String> reverseByStack( Collection<String> theList)
		   	Stack<String>theStack = new Stack<String>();
			Collection<String> reversed = new ArrayList<String>();
			for(String str : theList) { theStack.push(str); }			 
			while(!theStack.isEmpty()) { reversed.add(theStack.pop()); }
			return reversed;

Producer and consumer threads

The List may be read into a Queue, starting from the end and the contents of the Queue processed by another thread. This is an example of the Producer-consumer pattern. I did a similar thing in Python to find cycles in the behaviour of an automaton.

The Consumer and producerThread classes have identical fields and identical constructors that take a Queue and a List. I omitted the constructors and accessors for clarity

The producer thread implements Runnable and a synchronised run() method. The constructor takes a Queue. The run method just populates the queue in reverse order.

The consumer thread populates its collection with the list stored in the Queue. The run method is

Producer Thread code

public class ProducerThread implements Runnable
	private Queue<String> itsqueue;
	private List<String> itscollection;
// Add the collection to the queue in reverse order
	public synchronized void run()
	  for( int index = itscollection.size()-1; index>=0; index--)

Run method of consumer thread

	public synchronized void run()

permutation based reversals

The next step is to treat reversal as a permutation, which it is. This needed a way to apply a given permutation to the list and led to a consideration of Parallel Data transforms which I will discuss later.

To apply a permutation simply specify where each element of the list is to go. This cannot be done in place and trades efficiency for flexibility.

List permutation code

 * Performs the specified permutation on the given list 
 * List and permutation must be same size and not null
 * @param theList
 * @param permutation (of {0....theList.size()-1} )
 * @return permuted list
	public static List<String> permute(List<String> theList,List<Integer> permutation)
		List<String> result = new ArrayList<String>(permutation.size());
		for(int ii= 0; ii< permutation.size(); ii++)
		String value=null;
    for( int i=0; i < permutation.size(); i++ )
    	value = theList.get(i);
    	int destination = permutation.get(i).intValue();
    	result.set(destination, value);    	  	
    return result;

Shuffle based reversals

One useful permutation is a shuffle. As a concrete example consider a set of 8 elements


There are two ways to interleave the elements as in a perfect card shuffle




The respective permutations are {0,2,4,6,1,3,5,7} and {1,3,5,7,0,2,4,6};

The second shuffle is of interest here since all elements change position. The important point is that after three applications of this permutation to the original set the order of the elements is reversed. Which brings us to parallel data transform

Parallel Data Transforms

Parallel Data transforms were originally developed for the ICL Distributed Array Processor, a 64 by 64 grid of single bit processors with nearest neighbour connections which functioned as a Single Instruction Multiple Data computer. They were originally devised by Stuart Reddaway who deserves great respect not only for devising them but retaining his sanity while doing so. Reddaway also managed to find efficient ways to implement the shuffles above.

The idea behind parallel data transforms is simple. The indices in an array of 2N elements may be represented as N-bit unsigned integers. A useful subgroup of the permutations of the elements is the permutation group obtained by performing a common operation on the bits of each of the indices. For example the first shuffle above is obtained by a cyclic shift of the bits, and the second by flipping the most significant bit and following this with a cyclic shift of the bits. Reversal simply means flipping all bits of each index. The operations on the N bits of the indices yield N! Permutations without bit flipping and when bit flipping is allowed the total number of permutations rises to N!log2N.

Now it is obvious that if we keep flipping the most significant bit of an N-digit number and cyclically shifting the result then N repetitions are the same as a bitwise NOT and the net result is a reversal. On a serial machine this will take N Log2N operations but if there is an efficient hardware unit available to carry out the shuffles in negligible time, for a fixed array of 2N elements then reversal will take Log2N parallel operations. Of course there is limited demand for reversal but these shuffles have been used as the basis for parallel sorting algorithms and, as I recall, for parallel Fast Fourier Transform algorithms.

And for now this is about as far as I want to take List reversal though it would be fun to look again at more permutations using Parallel Data Transforms and in particular multidimensional versions.

The important point to note, especially for Architects, is that use of specialised hardware could radically change the characteristics of algorithms which in turn will affect the design of the system.

Summing up

Various methods of reversing a List in Java have been given. They sidestep problems with reversing Linked Lists and start with simple reversal, which is o(N) and proceed to more elegant methods. On a standard serial architecture the complexity of the algorithms varies from o(N) to o( Nlog2N). By creating dedicated hardware to perform shuffles the complexity can be reduced to log2N. The take home points for architects are that they need to experiment with different ways of carrying out a task “Is there another way”[1] and that dedicated hardware may radically alter the complexity of a task. Dedicated hardware can also reduce total lifecycle cost by eliminating maintenance for the tasks carried out by the hardware, once the hardware has been thoroughly debugged.


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)