Swap elements in LinkedList

前端 未结 7 1773
别那么骄傲
别那么骄傲 2020-12-20 14:32

I want to maintain order of the elements being added in a list. So, I used a LinkedList in Java.

Now I want to be able to swap two elements in the linke

相关标签:
7条回答
  • 2020-12-20 15:34

    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.

    0 讨论(0)
提交回复
热议问题