题目描述
输入一个链表,输出该链表中倒数第k个结点
题目链接:https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if (head == null) return head;
ListNode first = head;
ListNode sec = head;
for (int i = 0; i < k; i++) {
sec = sec.next;
if (sec == null && i != (k-1)) return null;
}
// 要的是正数第一个
if (sec == null) return head;
// 否则
while (sec != null) {
first = first.next;
sec = sec.next;
}
return first;
}
}