package com.thinker.test.test2;

/**
 * @author lzh
 * 题目：
 *		找到单向链表倒数第k个数字。
 *
 */
public class Test_2_2 {
    private static int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
    private static Node root;
    static class Node {
        public Node(int data) {
            this.data = data;
        }

        Node next;
        int data;
    }
    public static Node init(int i) {
        if(i>=arr.length){
            return  null;
        }
        Node node = new Node(arr[i]);
        Node newNode = init(i+1);
        if(newNode != null){
            node.next = newNode;
        }
        return node;
    }
    public static Node find(int k){
        if(k<0) return null;
        Node font = root;
        Node last = root;
        for(int i=0;i<k;i++){
            if(font == null) return null;
            font = font.next;
        }
        while(font != null){
            font = font.next;
            last = last.next;
        }
        return last;
    }
    public static void main(String[] args) {
        root = init(0);
        Node last = find(3);
        while(last != null){
            System.out.print(last.data+"\t");
            last = last.next;
        }
    }
}
