反转单向链表的几种方法

来源:互联网 发布:JS授权系统源码 编辑:程序博客网 时间:2024/06/05 12:39

1、非递归

    public Node reverse(Node current){        Node previousNode =null;        Node nextNode=null;        while(current!=null){            nextNode=current.next;            current.next=previousNode;            previousNode=current;            current=nextNode;        }        return previousNode;    }

2、递归

    public Node reverse1(Node current){        if(current==null||current.next==null)            return current;        Node nextNode=current.next;        current.next=null;        Node reverseRest=reverse(nextNode);        nextNode.next=current;        return reverseRest;    }
原创粉丝点击