如何反向链接列表? [英] How can I reverse a linked list?

查看:119
本文介绍了如何反向链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

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

如何准确反转列表?

我知道它首先将第二个节点设置为forward.然后说current.next等于null节点previous.然后说previous现在是current.最后current变成forward?

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 it's reversing. Can someone please explain how this works?

推荐答案

这篇关于如何反向链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆