输入一个链表,输出该链表中倒数第k个结点。
题解:
快慢指针,平移
第二种解法
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if (head == null || k <= 0) return null;
ListNode slow = head;
ListNode fast = head;
while(k > 0){
if(fast == null){
return null;
}
fast = fast.next;
k--;
}
while(fast != null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}