https://leetcode-cn.com/problems/kth-node-from-end-of-list-lcci/

    1. /**
    2. * Definition for singly-linked list.
    3. * public class ListNode {
    4. * int val;
    5. * ListNode next;
    6. * ListNode(int x) { val = x; }
    7. * }
    8. */
    9. class Solution {
    10. public int kthToLast(ListNode head, int k) {
    11. ListNode p=head, pre = head;
    12. while(k-- > 0){
    13. p=p.next;
    14. }
    15. while(p != null){
    16. pre = pre.next;
    17. p=p.next;
    18. }
    19. return pre.val;
    20. }
    21. }