/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ classSolution { public ListNode getKthFromEnd(ListNode head, int k) { ListNodep= head; intn=0; while (p != null) { ++n; p = p.next; } p = head; for (inti=1; i <= n - k; ++i) { p = p.next; } return p; } }