java暴力解法 不需要双指针,快捷迅速
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
public ListNode FindKthToTail (ListNode pHead, int k) {
// write code here
ListNode temp=pHead;
int i=0;
while(temp!=null){
temp=temp.next;
i++;
}
if (i<k){
//ListNode vall=new ListNode(null);
return null;
}
if (i==k){
return pHead;
}
int n=i-k;
while(n!=0){
pHead=pHead.next;
n--;
}
return pHead;
}
}
京公网安备 11010502036488号