1 精要
本质还是 双指针
1.1 所谓的模板
1.2 java语法处理
1.3 more 其他双指针的总结:
来自西加 https://leetcode-solution-leetcode-pp.gitbook.io/leetcode-solution/91/two-pointers
2 code
public ListNode FindKthToTail (ListNode pHead, int k) {
if(pHead==null || k<=0 )
return null;
int len = GetLen(pHead);
if(k>len)
return null;
// write code here
ListNode fast = pHead, slow = pHead;
//move k
int step = k;
while(step!=0){
fast= fast.next;
step--;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
int GetLen(ListNode pH){
if(pH==null)return 0;
int l =0;
while(pH!=null){
l++;
pH= pH.next;
}
return l;
}