如何扭转链表? [英] How to reverse a linked list?

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

问题描述

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

究竟是怎么也扭转了名单?我得到它首先将在第二个节点为转发。然后它说 current.next 等于节点 previous 。然后它说 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屋!

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