题目描述
输入一个链表,输出该链表中倒数第k个结点。
public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
//{}
if(head == null)
return head;
//{1,2,3} 0
if(k == 0)
return null;
ListNode res = head;
ListNode cur = head;
for(int i=1; i<=k-1; i++){
cur = cur.next;
//{1,2,3} 4
if(cur == null)
return cur;
}
while(cur != null && cur.next != null){
cur = cur.next;
res = res.next;
}
return res;
}
}