Java 双墨水列表的故障跟踪反转功能
我有一个完整的工作方法来反转双链接列表。老实说,几个月来我一直在回溯第四个月,试图追踪这段代码,看看它到底是如何工作的,但是当我用current.prev更新我的当前节点时,我在while的末尾感到困惑 我试图在每次更改下一个和上一个指针时打印出节点的值,但是我得到了一个nullpointerexception,所以没有运气Java 双墨水列表的故障跟踪反转功能,java,data-structures,linked-list,doubly-linked-list,Java,Data Structures,Linked List,Doubly Linked List,我有一个完整的工作方法来反转双链接列表。老实说,几个月来我一直在回溯第四个月,试图追踪这段代码,看看它到底是如何工作的,但是当我用current.prev更新我的当前节点时,我在while的末尾感到困惑 我试图在每次更改下一个和上一个指针时打印出节点的值,但是我得到了一个nullpointerexception,所以没有运气 public void reverse(){ Node temp = null; Node current = head; while(cur
public void reverse(){
Node temp = null;
Node current = head;
while(current != null){
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if(temp != null){
head = temp.prev;
}
}
这里没有错误,我通过我自己的测试用例通过了最坏和最好的场景。我只是不明白发生了什么。我知道这实际上是交换下一个和上一个指针,但我需要知道如何交换
public void reverse(){
// Create initial values
Node temp = null;
// Note that you are using current to traverse through the linked list
Node current = head;
// While current is not at the end of the original (non-reversed) list
while(current != null){
// Swapping prev and next
temp = current.prev; // temp 'temporarily' holds copy of current.prev
current.prev = current.next; // current.prev is overwritten with current.next
current.next = temp; // current.next is overwritten with temp (containing original current.prev)
// You are setting current to the newly redefined prev
// This was equal to current->next in the original (non-reversed) list
// So you are traversing through the original list
// Anything 'before' this has already been reversed
// Anything 'after' still needs to be reversed
current = current.prev;
}
// Condition checks for edge case of a one node linked list
if(temp != null){
// Set the head of the reversed list
head = temp.prev;
}
上面的注释代码。我不是Java程序员,但在C语言中,我会在反转前后打印出每个节点的地址,以检查我是否做得正确。也许,你可以用做类似的事情
上面的注释代码。我不是Java程序员,但在C语言中,我会在反转前后打印出每个节点的地址,以检查我是否做得正确。也许,您可以使用来执行类似的操作?这类似于Java中的交换:
while(current != null){
temp = current.prev; //gets the 'end' of the doubly linked list
current.prev = current.next; //sets the 'end' of the doubly linked list to the 'first' of the list
current.next = temp; //sets the 'first' of the list to temp, which is the 'end' of the list
current = current.prev; //iterate through the list in reverse order (similar to i++ in for loops)
}
这类似于Java中的交换:
while(current != null){
temp = current.prev; //gets the 'end' of the doubly linked list
current.prev = current.next; //sets the 'end' of the doubly linked list to the 'first' of the list
current.next = temp; //sets the 'first' of the list to temp, which is the 'end' of the list
current = current.prev; //iterate through the list in reverse order (similar to i++ in for loops)
}
那么,您的问题是NullPointerException?考虑到抛出了异常,说“此处没有错误”并不是真的。您知道如何使用IDE的调试器吗?因此,如果我尝试使用System.out.print()进行跟踪,则会抛出此异常,因为在某些情况下,该值为null,不允许我打印它们。否则,如果我尝试像node.next.next.next或node.prev.prev.prev这样的东西,它告诉我这个列表是一个双链接列表,在反转它时不会丢失节点。不,我不知道,我对使用VSCode相当陌生,那么今天将是学习的好日子。通过调试器,您可以逐行遍历程序,并在每个点查看每个变量的值。它非常适合这种情况。如果你想成为一名专业程序员,你的调试器将在你的职业生涯中为你节省数年的时间。那么,你的问题是NullPointerException?考虑到抛出了异常,说“此处没有错误”并不是真的。您知道如何使用IDE的调试器吗?因此,如果我尝试使用System.out.print()进行跟踪,则会抛出此异常,因为在某些情况下,该值为null,不允许我打印它们。否则,如果我尝试像node.next.next.next或node.prev.prev.prev这样的东西,它告诉我这个列表是一个双链接列表,在反转它时不会丢失节点。不,我不知道,我对使用VSCode相当陌生,那么今天将是学习的好日子。通过调试器,您可以逐行遍历程序,并在每个点查看每个变量的值。它非常适合这种情况。如果你想成为一名专业程序员,你的调试器将在你的职业生涯中为你节省数年的时间。