如何反转链表? [英] How to reverse a linked list?
本文介绍了如何反转链表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
节点前一个=空;
Node current = head;
节点转发;
while(current!= null){
forward = current.next;
current.next = previous;
以前=当前;
current = forward;
}
返回上一个;
}
它是如何扭转列表的?我得到它首先将第二个节点设置为 forward
。然后它说 current.next
等于一个 null
node previous
。然后它说以前的
现在是当前
。最后当前
成为转发
?
我不能似乎把握了这个以及它的逆转。有人可以解释一下这个工作原理吗?
解决方案
Node reverse(Node head) {
Node previous = null;
Node current = head;
Node forward;
while (current != null) {
forward = current.next;
current.next = previous;
previous = current;
current = forward;
}
return previous;
}
How exactly is it reversing the list? I get that it first sets the second node to forward
. Then it says current.next
is equal to a null
node previous
. Then it says previous
is now current
. Lastly current
becomes forward
?
I can't seem to grasp this and how its reversing. Can someone please explain how this works?
解决方案
这篇关于如何反转链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文