标签:sig return subject tla orm turn nod 题目 list
#include <iostream> #include <algorithm> #include "string.h" #include "stdio.h" #include <vector> #include <deque> #include<stack> using namespace std; struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class List{ public: ListNode* CreatList(int* arr,int len) { int val; ListNode* pHead = new ListNode(arr[0]); ListNode* pCurrent=NULL; ListNode* rear = pHead; int count = 1; while(count<len) { ListNode* pCurrent = new ListNode(arr[count]); rear->next = pCurrent; rear = pCurrent; count++; } rear->next = NULL; return pHead; } void ShowList(ListNode* pHead) { while(pHead) { cout<<pHead->val<<" "; pHead = pHead->next; } cout<<endl; } ListNode* GetLastNode(ListNode* pHead) { ListNode* pNode = pHead; while(pNode->next!=NULL) { pNode=pNode->next; } return pNode; } }; class Sort{ public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { if(pListHead == NULL||k==0) return NULL; ListNode* pNode = pListHead; ListNode* p = pListHead; int count = 0; pNode=pListHead; for(int i=0;i<k-1;i++) { if(pNode->next!=NULL) pNode = pNode->next; else return NULL; } while(pNode->next!=NULL) { p = p->next; pNode=pNode->next; } return p; } }; int main() { int array[]={3,4,5,1,2,8,7}; List list; Sort sort; ListNode* pHead = list.CreatList(array,sizeof(array)/sizeof(array[0])); list.ShowList(pHead); ListNode* pNode = sort.FindKthToTail(pHead,7); cout<<pNode->val<<endl; return 0; }
标签:sig return subject tla orm turn nod 题目 list
原文地址:http://www.cnblogs.com/omelet/p/6653557.html