【问题标题】:Trouble tracing reverse function for doubly inked list [duplicate]双向链表的反向功能跟踪问题[重复]
【发布时间】:2019-08-21 06:02:23
【问题描述】:

我有一个完整的工作方法来反转一个双向链表。老实说,几个月来我一直在回去和第四次尝试跟踪这段代码以确切了解它是如何工作的,但是当我用 current.prev 更新当前节点时,我在 while 结束时感到困惑

我尝试在每次更改下一个和前一个指针时打印出节点的值,但是我得到一个空指针异常,所以那里没有运气。

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;
    }
}

这里没有错误,我通过我自己的测试用例来测试最坏和最好的情况。我似乎无法理解发生了什么。我知道这实际上是在交换 next 和 prev 指针,但我需要知道如何。

【问题讨论】:

  • 那么,你的问题是 NullPointerException 吗?考虑到抛出异常,说“这里没有错误”并不是真的。
  • 你知道如何使用你的 IDE 的调试器吗?
  • 因此,如果我尝试使用 System.out.print() 进行跟踪,则会引发此异常,因为在某些时候该值为 null 并且不允许我打印它们。否则,如果我尝试类似 node.next.next.next 或 node.prev.prev.prev 之类的东西,它告诉我该列表是一个双向链表,并且在反转它时不会丢失节点。
  • 不,我不知道,我对使用 VSCode 很陌生
  • 那么今天是学习的好日子。您的调试器允许您逐行执行程序,并查看每个变量在每个点的值。它非常适合这种情况。如果您要成为一名专业程序员,您的调试器将在您的职业生涯中为您节省数年的时间。

标签: java data-structures linked-list doubly-linked-list


【解决方案1】:
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 语言中,我会在反转前后打印出每个节点的地址,以检查我做事是否正确。或许,您可以使用hashcodes 做类似的事情?

【讨论】:

    【解决方案2】:

    这类似于 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)
        }
    
     if(temp != null){
            head = temp.prev;  //edge case when there is only 1 node.
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多