leetcode-剑指22-OK

address

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* getKthFromEnd(struct ListNode* head, int k){
    int getlenth(struct ListNode* head){
        int len = 0;
        while(head!=NULL){
            len++;
            head = head->next;
        }
        return len;
    }
    k = getlenth(head)-k;
    while(k>0){
        k--;
        head = head->next;
    }
    return head;
}
posted @ 2021-01-21 07:01  RougeBW  阅读(28)  评论(0编辑  收藏  举报