题解 | #链表中倒数第k个结点#
链表中倒数第k个结点
http://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a
/**
- struct ListNode {
- int val;
- struct ListNode *next;
- };
- C语言声明定义全局变量请加上static,防止重复定义 */
/** *
- @param pListHead ListNode类
- @param k int整型
- @return ListNode类 / struct ListNode FindKthToTail(struct ListNode* pListHead, int k ) { // write code here int copy_k = k; struct ListNode* slow = pListHead; struct ListNode* fast = pListHead; if( !pListHead || !k ) { return NULL;} while( 1 ) { copy_k = k; fast = slow; while( copy_k-- && fast ) { fast = fast->next ; } if( copy_k >=0 ) {return NULL;} if( !fast) { return slow;} slow = slow->next ; }
}