Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何实现链表的逆序。 #500

Open
pwstrick opened this issue Jul 16, 2019 · 1 comment
Open

如何实现链表的逆序。 #500

pwstrick opened this issue Jul 16, 2019 · 1 comment
Labels
算法 算法类的题目

Comments

@pwstrick
Copy link
Owner

如何实现链表的逆序。

@pwstrick pwstrick added the 算法 算法类的题目 label Jul 16, 2019
@doflamin
Copy link

    function Node(value){//创建节点类
        this.value = value;
        this.node = null
    }

    //创建五个链表节点
    var node1 = new Node(1);
    var node2 = new Node(2);
    var node3 = new Node(3);
    var node4 = new Node(4);
    var node5 = new Node(5);

    //将五个节点连接起来
    node1.next = node2;
    node2.next = node3;
    node3.next = node4;
    node4.next = node5;

    //通过递归方式,将链表顺序逆转
    function reserveList(root){
        //下面的if条件是递归的结束条件
        if(root.next.next == null){//判断是否是倒数第二个节点
            root.next.next = root;//将最后一个节点的next指向倒数第二个节点
            return ;
        }else{//不是倒数第二个节点
            reserveList(root.next);//继续递归
            root.next.next = root ;//将下一个节点的next指向当前节点
            root.next = null;//将当前节点的next指向null
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
算法 算法类的题目
Projects
None yet
Development

No branches or pull requests

2 participants