我有以下代码来反转链表:
node old = head;
head = null;
while (old!=null) {
node temp = old.link;
old.link = head;
head = old;
old = temp;
}
有人可以解释一下这段代码的每一行,因为我试图通过绘制方框图来看看这是如何反转列表的,但我仍然不明白。
答案 0 :(得分:7)
假设head
是指向列表头部的指针(1,2,3,4):
+-----+ link +-----+ link +-----+ link +-----+ link
| 1 | ---> | 2 | ---> | 3 | ---> | 4 | ---> null
+-----+ +-----+ +-----+ +-----+
^ head
node old = head;
head = null;
+-----+ link +-----+ link +-----+ link +-----+ link
| 1 | ---> | 2 | ---> | 3 | ---> | 4 | ---> null
+-----+ +-----+ +-----+ +-----+
^ old
null
^ head
(while
循环的第一次迭代......)
node 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
head = old;
+-----+ link +-----+ link +-----+ link +-----+ link
| 1 | -+ | 2 | ---> | 3 | ---> | 4 | ---> null
+-----+ | +-----+ +-----+ +-----+
^ old | ^ temp
^ head |
+----------------------------------------> null
old = temp;
+-----+ link +-----+ link +-----+ link
| 2 | ---> | 3 | ---> | 4 | ---> null
+-----+ +-----+ +-----+
^ old
^ temp +-----+
| 1 | ---> null
+-----+
^ head
(第二次迭代......)
node 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
head = old;
+-----+ link +-----+ link +-----+ link
| 2 | -+ | 3 | ---> | 4 | ---> null
+-----+ | +-----+ +-----+
^ old | ^ temp
^ head | +-----+ link
+--------------> | 1 | ---> null
+-----+
old = temp;
+-----+ link +-----+ link
| 3 | ---> | 4 | ---> null
+-----+ +-----+
^ old
^ temp
+-----+ link +-----+ link
| 2 | ---> | 1 | ---> null
+-----+ +-----+
^ head
重复直到old
指向末尾的空(即,直到原始列表为空)。
答案 1 :(得分:4)
+-----+ +-----+ +-----+
head-->| |-->| |-->| |-->null
| A | | B | | C |
| | | | | |
+-----+ +-----+ +-----+
node old = head
+-----+ +-----+ +-----+
head-->| |-->| |-->| |-->null
| A | | B | | C |
| | | | | |
+-----+ +-----+ +-----+
^^^
old
head = null
+-----+ +-----+ +-----+
head | |-->| |-->| |-->null
vvvv | A | | B | | C |
null | | | | | |
+-----+ +-----+ +-----+
^^^ ^^^^
old temp
node temp = old.link
+-----+ +-----+ +-----+
head | |-->| |-->| |-->null
vvvv | A | | B | | C |
null | | | | | |
+-----+ +-----+ +-----+
^^^ ^^^^
old temp
old.link = head
+-----+ +-----+ +-----+
head | | | |-->| |-->null
vvvv | A | | B | | C |
null<--| | | | | |
+-----+ +-----+ +-----+
^^^ ^^^^
old temp
head = old
+-----+ +-----+ +-----+
head-->| | | |-->| |-->null
| A | | B | | C |
null<--| | | | | |
+-----+ +-----+ +-----+
^^^ ^^^^
old temp
Rearranging slightly
old = temp
+-----+
| |
| A |
head-->| |-->null
+-----+
+-----+ +-----+
old-->| |-->| |-->null
| B | | C |
| | | |
+-----+ +-----+
^^^^
temp
Note that A is now a valid (if short) linked list.
node temp = old.link
+-----+
| |
| A |
head-->| |-->null
+-----+
+-----+ +-----+
old-->| |-->| |-->null
| B | | C |
| | | |
+-----+ +-----+
^^^^
temp
old.link = head
+-----+
| |
| A |
head-->| |-->null
+-----+
^
|
+-----+ +-----+
old-->| | | |-->null
| B | | C |
| | | |
+-----+ +-----+
^^^^
temp
head = old
+-----+
| |
| A |
| |-->null
+-----+
^
|
+-----+ +-----+
old-->| | | |-->null
| B | | C |
head-->| | | |
+-----+ +-----+
^^^^
temp
Rearrange,
old = temp
+-----+ +-----+
| | | |
| B | | A |
head-->| |-->| |-->null
+-----+ +-----+
+-----+
old-->| |-->null
| C |
| |
+-----+
^^^^
temp
Guess what? The sub-list A-B is now a valid list B-A.
node temp = old.link
+-----+ +-----+
| | | |
| B | | A |
head-->| |-->| |-->null
+-----+ +-----+
+-----+
old-->| |-->null
| C | ^
| | |
+-----+ |
|
temp
old.link = head;
+-----+ +-----+
| | | |
| B | | A |
head-->| |-->| |-->null
+-----+ +-----+
^
|
+-----+
old-->| |-->null
| C | ^
| | |
+-----+ |
|
temp
head = old;
+-----+ +-----+
| | | |
| B | | A |
| |-->| |-->null
+-----+ +-----+
^
|
+-----+
old-->| | null
| C | ^
head-->| | |
+-----+ |
|
temp
old = temp;
+-----+ +-----+ +-----+
| | | | | |
| C | | B | | A |
head-->| |-->| |-->| |-->null
+-----+ +-----+ +-----+
old-->null<--temp
Old is null, break out of the loop, we're done!
答案 2 :(得分:3)
有助于绘制每行代码的内容
希望你能遵循这个,我将每行代码编号到循环1-3,然后循环内部是A,B,C,D,如下所示:
1. Node old = head;
2. head = null;
3. while (old!=null) {
A. Node temp = old.link;
B. old.link = head;
C. head = old;
D. old = temp;
}
编辑:Woops它削减了底部图表一点点。 Head应指向新的第一个节点(图中的最后一个节点)。临时和旧都应该是空的。以前的节点,现在指向Null,因为它现在是最后一个节点。因此它成功地逆转了。