Swap elements in LinkedList

前端 未结 7 1798
别那么骄傲
别那么骄傲 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:25

    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.

提交回复
热议问题