/*递归*/ int recursion(struct ListNode* head, int k, int* num){ if (!head) { (*num)=1; return 100000; } int ret = recursion(head->next, k, num); if (ret == 100000 && (*num)++ == k) ret=head->val; return ret; } int kthToLast(struct ListNode* head, int k){ int num=0; return recursion(head,k,&num);; }
/*快慢指针*/ int kthToLast(struct ListNode* head, int k){ struct ListNode*t = head; while (k--){ t = t->next; } while (t){ t = t->next; head = head->next; } return head->val; }