题目描述
输入一个链表,输出该链表中倒数第k个结点。
…以前做过了,就是快慢指针…不过这个题因为没有考虑周期…多提交了几次
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null || k <= 0){
return null;
}
ListNode cur1 = head;
ListNode cur2 = head;
for(int i = 0;i<k-1;i++){
if(cur1.next == null){
return null;
}
cur1 = cur1.next;
}
while(cur1.next != null){
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur2;
}
}