剑指 Offer 22. 链表中倒数第k个节点
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 9 10 struct ListNode* getKthFromEnd(struct ListNode* head, int k){ 11 int count = 0; 12 struct ListNode *p = head; 13 while(p) 14 { 15 count++; 16 p=p->next; 17 } 18 19 count = count-k+1; 20 int sum=0; 21 while(head){ 22 sum++; 23 if(sum==count) 24 { 25 break; 26 } 27 28 head = head->next; 29 } 30 31 return head; 32 }