剑指offer No.15,这题也是同样的双指针,代码如下
package com.zjy.sword2offer;
public class KthtoTail {
public ListNode kthtoTail(ListNode head, int k){
if(head==null||k<=0)
return null;
ListNode front = head;
ListNode back = head;
for(int i=0;i<k-1;i++)
{
if(front.next!=null)
front = front.next;
else
return null;
}
while(front.next!=null)
{
front = front.next;
back = back.next;
}
return back;
}
}