public class Solution {
public ListNode FindKthToTail(ListNode head,int k) {
if(head==null) {
return null;
}
if(k<=0) {
return null;
}
ListNode fast = head;
ListNode slow = head;
while(k-1>0) {
if(fast.next!=null) {
fast = fast.next;
k--;
}
else{
return null;
}
}
while(fast.next!=null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
输入一个链表,输出该链表中倒数第k个结点。
最新推荐文章于 2024-08-02 18:52:04 发布