首先一次遍历计算出链表长度;然后从头开始遍历,遍历到k-i就返回(i为链表下标,从0开始)
public ListNode FindKthToTail (ListNode pHead, int k) { // write code here //先计算出长度,然后循环判断 ListNode dummy = new ListNode(0); dummy.next = pHead; ListNode temp = pHead; int len = 0; while(temp != null){ len++; temp = temp.next; } ListNode cur = dummy.next; int i = 0; while(cur != null){ if(k == len - i){ return cur; }else{ cur = cur.next; i++; } } return null; }