Swap elements in LinkedList

There is a Collections.swap(List<?> list, int i, int j) that you can use to swap two elements of a List<?>. There's also LinkedList.get(int index) and LinkedList.add(int index, E element) (both are methods specified by interface List). All of these operations will be O(N) since a LinkedList does not implements RandomAccess.


Check out the Javadocs for LinkedList

To find an element at an index use get(int index)

To place an element at a certain index use set(int index, Object element)


If you are writing your own LinkedList class for exercise (i.e. for a project or school), try making two temporary Object variables and two ints to hold their position in the List. Then, use add(int, Object) to add the first in the 2nd position, second in the 1st position.