The sword refers to Offer_Programming questions_14

Topic description

Input a linked list and output the k-th node from the bottom of the linked list.
/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
        ListNode* list = pListHead;
        int len ​​= 0;
        while(list!=NULL){
            list=list->next;
            len ++;
        }
        len = len-k;
        list = pListHead;
        if(len>=0){
            while(len--){
                list=list->next;
            }
            return list;
        }else
            return NULL;
    }
};

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737495&siteId=291194637