给定一个链表: 1->2->3->4->5, 和 k = 2.
返回链表 4->5.
思路:快慢指针法,快的也走K步,然后快慢同步走,快的走到头时,慢的位置即答案。
ListNode* moveK(ListNode *cur, int k) {for (int i = 0; i < k; i++) {cur = cur->next;}return cur;}ListNode* getKthFromEnd(ListNode* head, int k) {ListNode *slow = head;ListNode *cur = moveK(head, k);while (cur) {slow = slow->next;cur = cur->next;}return slow;}
