题目描述

image.png

解题思路

具体参照专题的链表章节。

  1. class Solution {
  2. public ListNode getKthFromEnd(ListNode head, int k) {
  3. ListNode pre = head;
  4. ListNode cur = head;
  5. while (k-- > 0) {
  6. cur = cur.next;
  7. }
  8. while (cur != null) {
  9. cur = cur.next;
  10. pre = pre.next;
  11. }
  12. return pre;
  13. }
  14. }