在这里插入代码片在这里插入代码片/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
private int getSize(ListNode head){
int size =0;
for(ListNode cur = head; cur != null;cur = cur.next){
size ++;
}
return size;
}
public ListNode FindKthToTail(ListNode head,int k) {
if(head == null){
return null;
}
//需要保证K的值有效
int size = getSize(head);
if(k <= 0 || k > size){
return null;
}
//引用走的步数
int offset = size - k;
ListNode cur = head;
for(int i = 0;i < offset; i++){
cur = cur.next;
}
return cur;
}
}
03-12
236
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-16
07-16
07-16
07-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交