1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode FindKthToTail(ListNode head,int k) { 12 if(head==null || k<=0){ 13 return null; 14 } 15 ListNode pre=head; 16 ListNode last=head; 17 for(int i=1;i<k;i++){ 18 if(pre.next!=null){ 19 pre=pre.next; 20 }else{ 21 return null; 22 } 23 } 24 while(pre.next!=null){ 25 pre=pre.next; 26 last=last.next; 27 } 28 return last; 29 } 30 }
原文:https://www.cnblogs.com/wangqiong/p/11761900.html