题解 | #链表中倒数最后k个结点# 快慢指针法
链表中倒数最后k个结点
http://www.nowcoder.com/practice/886370fe658f41b498d40fb34ae76ff9
public ListNode FindKthToTail (ListNode head, int k) { if(head==null) return head; // 先定义一个prev前缀节点 ListNode prev = new ListNode(0); prev.next = head; ListNode slow = prev, fast = head; // 先让fast走k步 while(k-->0&&fast!=null){ fast = fast.next; } if(fast==null&&k>=0) return null; while(fast!=null){ fast = fast.next; slow = slow.next; } return slow.next; }