package com.alipay.sofa.isle.sample; class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } public class Solution { public static ListNode FindKthToTail(ListNode head,int k) { ListNode res = new ListNode(0); find(head ,k ,res); return res.val == 0 ? null : res; } public static int find(ListNode head,int k ,ListNode res) { if(head == null) { return 1; } else { int count = find(head.next , k ,res); if(count == k) { res.val = head.val; } else if(count < k){ res.next = head; res = head; } return count + 1; } } public static void main(String[] args) { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); ListNode node3 = new ListNode(3); ListNode node4 = new ListNode(4); ListNode node5 = new ListNode(5); node1.next = node2; node2.next = node3; node3.next = node4; node4.next = node5; FindKthToTail(node1 , 5); } }