题解 | #链表中倒数最后k个结点#
链表中倒数最后k个结点
http://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
public ListNode FindKthToTail (ListNode pHead, int k) { // write code here if (pHead == null || k < 1) { return null; } // 快指针 ListNode fast = pHead; // 找列表中的第k个节点 for (int i = 1; i < k; i++) { fast = fast.next; // 列表长度小于k if (fast == null) { return null; } } // 慢指针 ListNode slow = pHead; while (fast.next != null) { fast = fast.next; slow = slow.next; } return slow; }