依旧是双指针,而且回顾了一下反转链表发现还是不会,要加深对双指针的印象
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode pre = head, cur = head;
for(int i=0; i<k; i++){
cur = cur.next;
}
while(cur != null){
cur = cur.next;
pre = pre.next;
}
return pre;
}
}