1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Solution { public class ListNode { int val; ListNode next = null;
ListNode(int val) { this.val = val; } } public ListNode FindKthToTail(ListNode head,int k) { if(head==null||k==0){ return null; } ListNode head1 = head; ListNode head2 = head; int i=1; while (head1.next!=null&&k>i++) { head1 = head1.next; } if(k>i){ return null; } while(head1.next!=null){ head1 = head1.next; head2 = head2.next; } return head2; } }
|