我正在阅读有关java实现中的队列。我要实现以下任务:颠倒整个列表的顺序为双向linkedList
public class DoublyLinkedList
{
private Node first; // the first Node in the list
private Node last; // the last Node in the list
private class Node
{
private Point p;
private Node prev; // the previous Node
private Node next; // the next Node
}
public void reverse()
{
// your code
}
}
我不喜欢这样的:
public void reverse() { // that reverses the order of the entire list
if (first == null && last == null) {
throw new RuntimeException();
}
Node current = first;
while (current!=null) {
current.next= current.next.prev;
current.prev=current.prev.next;
current=current.next;
}
}
我这样做对吗? 谢谢
不会如预期你有工作吗? – dave823
'扔新'...失去了一些东西。 – AxelH
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html – xenteros