Java - LinkedList中的"旋转"对象 - LinkedList.addLast(LinkedList.removeFirst())是好还是坏编程?

The*_*ing 5 java linked-list

在我的Java应用程序中,以下两个都将编译并运行,并产生所需的结果.

//"Rotate" the list items one place to the left.
myLinkedList.addLast(myLinkedList.removeFirst());
Run Code Online (Sandbox Code Playgroud)

并且在相反方向上"旋转"

//"Rotate" the list items one place to the right.
myLinkedList.addFirst(myLinkedList.removeLast());
Run Code Online (Sandbox Code Playgroud)

两个"旋转"每个只需要一行代码,但我想知道这是否是正确的方法呢?这种方法有什么缺陷吗?

是否有更好,更强大,更容易出错的方式,就像我上面所做的那样,需要多行代码才能实现,如果有,请解释原因.

Jon*_*eet 4

对我来说似乎不错。如果您有一个真正的循环缓冲区已满,可以只移动“开始/结束”索引,但我认为链表方法会工作得很好。特别是它仍然是 O(1)。