好吧,好像我正在试图扭转这个列表的无限循环。所以说我有一些随机数字,如4,5,6,7,我试图将其反转为7,6,5,4。我开始使用头部的节点并添加它以结束最后一个节点,直到我得到最终列表(IE 7,4 - > 7,5,4),但我尝试的每一个都给了我无限循环。
public void Reversing() {
Node<E> currently = this.head;//set it to head to loo[
Node<E> last = this.head;
Node<E> temp = this.head; //not used anymore
last=lastNode();//gets the last item in the list
while (currently != null) {//loop until not null
last.next=currently;
currently=currently.next;
if(last.info==currently.info){//break out of loop
break;
}
}
}
答案 0 :(得分:1)
您正在撤消单个链接列表。请参阅此SO问题,了解如何执行此操作Reverse Singly Linked List Java
我将在答案中复制:
Node<E> reversedPart = null;
Node<E> current = head;
while (current != null) {
Node<E> next = current.next;
current.next = reversedPart;
reversedPart = current;
current = next;
}
head = reversedPart;