阅读量:0
面试题 02.02. 返回倒数第 k 个节点 - 力扣(LeetCode)( 点击前面链接即可查看题目)
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ int kthToLast(struct ListNode* head, int k) { struct ListNode* fastnode = head; struct ListNode* slownode = head; while(k--) { fastnode = fastnode->next; } while(fastnode) { fastnode = fastnode->next; slownode = slownode->next; } return slownode->val; }