递归方法反转链表(Java实现)

class Node{
 int value;
 Node next;
 public Node(){}
 public Node(int value){
   this.value=value;
    }
}
public class Main{
    public static void main(String[] args){
        Node node = new Node(1);
        node.next=new Node(2);
        node.next.next=new Node(3);
        Node node1 = resvlor(node);
        while(node1!=null){
            System.out.println(node1.value);
            node1=node1.next;
        }
    }

    public static Node resvlor(Node head){
        if(head==null||head.next==null){
            return head;
        }
        Node newNode=resvlor(head.next);
        head.next.next=head;
        head.next=null;
        return newNode;
    }

你可能感兴趣的:(笔试遇到的算法题,链表,java,数据结构)