再帰的にリストを作成します。

再帰は数学的にコード化された数学的帰納の一種です

専門は: 1時00分確立N ==は、(コードでは、必ずしも特別な場合のn == 1ないN == 0、又はn == NULLができる)場合、一般化されたプロセスは、時々 、特別な状況に影響を与えます。だから、コードを書くときには、専門性を考慮して、再び最初の一般論理プロセスを終了しようとする必要があります。

一般化: N-1はまた、(書き込みプログラムで)確立されたnに関係を確立するための数学は、n-1を満たすも関係n個の確認を満たしています。

栗の場合:

次の二つの再帰的加算機能は右が、異なっています

//は異なる専門に異なる結果を一般化
INT(SUM INT {N-)// 自然数と1〜N見つける
IF(N - == 1リターン 1。 ; // 特定化
リターン SUM(N- 1)+ N-; /// 一般
} 
INT SUM1(INT N-){ // 自然数と1〜N見つける IF(N - == 1リターン 1。 ; // 専門 IF(N - == 2リターン 3。 戻り SUMを(N- 2)+ + N-N- 1; // 一般化 }

 

 java代码如下:
节点类定义:

package interview;

public class Node {
private final int value;
private Node next;

public Node(int value) {
    super();
    this.value = value;
    this.next = null;
}
public Node getNext() {
    return next;
}

public void setNext(Node next) {
    this.next = next;
    
}
public int getValue() {
    return value;
}

public static void printList(Node head){
while(head!=null){
    System.out.print(head.getValue());
    System.out.print(" ");
    head=head.getNext();    
}System.out.println();
}


}

主函数:

package interview;

import java.util.List;
import java.util.Arrays;
public class list {
    /**
     * Creats a Linked List    
     * 
     * @param data the data to creat the list
     * @return head of the linked list.The return linked list 
     * ends with last node with getNext() == null.
     */
    public Node creatList(List<Integer> data){
    
        if(data.isEmpty()){
            return null;
        }
        Node firstNode = new Node(data.get(0));
        Node headofSublist = creatList(data.subList(1,data.size()));
        firstNode.setNext(headofSublist);
        return firstNode;
        
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        list creator = new list();
        //Node.printList(new ArrayList<>());
        Node.printList(creator.creatList(Arrays.asList(1)));
        Node.printList(creator.creatList(Arrays.asList(1,2,3,4,5)));
        
        

    }

}

 

おすすめ

転載: www.cnblogs.com/cstdio1/p/11250359.html