快慢指针
快指针先走k步,再两个一块走。
java
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode slow = head, fast = head;
for(int i = 0; i < k; i++) fast = fast.next;
while(fast != null){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}