This question already has answers here:
How can I reverse a linked list?

(10个回答)


6年前关闭。





我有以下代码用于反向链接列表:

node old = head;
head = null;

while (old!=null) {
   node temp = old.link;
   old.link = head;
   head = old;
   old = temp;
}


有人可以解释一下这段代码的每一行,因为我试图通过绘制箱形图来查看这是如何反转列表的,但是我还是不明白。

最佳答案

假设head是指向列表头(1、2、3、4)的指针:

+-----+ link +-----+ link +-----+ link +-----+ link
|  1  | ---> |  2  | ---> |  3  | ---> |  4  | ---> null
+-----+      +-----+      +-----+      +-----+
 ^ head


节点old = head;
头=空;

+-----+ link +-----+ link +-----+ link +-----+ link
|  1  | ---> |  2  | ---> |  3  | ---> |  4  | --->  null
+-----+      +-----+      +-----+      +-----+
 ^ old

                                                     null

                                                      ^ head


while循环的第一次迭代...)
节点temp = old.link;

+-----+ link +-----+ link +-----+ link +-----+ link
|  1  | ---> |  2  | ---> |  3  | ---> |  4  | --->  null
+-----+      +-----+      +-----+      +-----+
 ^ old        ^ temp

                                                     null

                                                     ^ head


old.link = head;

+-----+ link +-----+ link +-----+ link +-----+ link
|  1  | -+   |  2  | ---> |  3  | ---> |  4  | --->  null
+-----+  |   +-----+      +-----+      +-----+
 ^ old   |    ^ temp
         |
         +---------------------------------------->  null

                                                     ^ head


头=老

+-----+ link +-----+ link +-----+ link +-----+ link
|  1  | -+   |  2  | ---> |  3  | ---> |  4  | --->  null
+-----+  |   +-----+      +-----+      +-----+
 ^ old   |    ^ temp
 ^ head  |
         +---------------------------------------->  null


旧=临时;

             +-----+ link +-----+ link +-----+ link
             |  2  | ---> |  3  | ---> |  4  | --->  null
             +-----+      +-----+      +-----+
              ^ old
              ^ temp                   +-----+
                                       |  1  | --->  null
                                       +-----+
                                        ^ head


(第二次迭代...)
节点temp = old.link;

             +-----+ link +-----+ link +-----+ link
             |  2  | ---> |  3  | ---> |  4  | --->  null
             +-----+      +-----+      +-----+
              ^ old        ^ temp
                                       +-----+ link
                                       |  1  | --->  null
                                       +-----+
                                        ^ head


old.link = head;

             +-----+ link +-----+ link +-----+ link
             |  2  | -+   |  3  | ---> |  4  | --->  null
             +-----+  |   +-----+      +-----+
              ^ old   |    ^ temp
                      |                +-----+ link
                      +--------------> |  1  | --->  null
                                       +-----+
                                        ^ head


头=老

             +-----+ link +-----+ link +-----+ link
             |  2  | -+   |  3  | ---> |  4  | --->  null
             +-----+  |   +-----+      +-----+
              ^ old   |    ^ temp
              ^ head  |                +-----+ link
                      +--------------> |  1  | --->  null
                                       +-----+


旧=临时;

                          +-----+ link +-----+ link
                          |  3  | ---> |  4  | --->  null
                          +-----+      +-----+
                           ^ old
                           ^ temp

                          +-----+ link +-----+ link
                          |  2  | ---> |  1  | --->  null
                          +-----+      +-----+
                           ^ head


重复til old指向末尾的null(即,直到原始列表为空)。

关于java - 了解如何反向链接列表? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19669843/

10-12 22:56