面试题15、输入一个链表,输出该链表中倒数第k个结点。

来源:互联网 发布:开源软件二次开发 编辑:程序博客网 时间:2024/06/05 23:08
/*brief: 输入一个链表,输出该链表中倒数第k个结点。*/#include "stdafx.h"#include "List.h"ListNode *FindKthToTail(ListNode *pListHead, unsigned int k){if(pListHead == NULL || k == 0)return NULL;ListNode *pAhead = pListHead;ListNode *pBehind = NULL;for(unsigned int i = 0; i < k - 1; ++i){if(pAhead->m_pNext != NULL)pAhead = pAhead->m_pNext;elsereturn NULL;}pBehind = pListHead;while(pAhead->m_pNext != NULL){pAhead = pAhead->m_pNext;pBehind = pBehind->m_pNext;}return pBehind;}// ====================测试代码====================// 测试要找的结点在链表中间void Test1(){printf("=====Test1 starts:=====\n");ListNode *pNode1 = CreateListNode(1);ListNode* pNode2 = CreateListNode(2);    ListNode* pNode3 = CreateListNode(3);    ListNode* pNode4 = CreateListNode(4);    ListNode* pNode5 = CreateListNode(5);ConnectListNodes(pNode1, pNode2);ConnectListNodes(pNode2, pNode3);    ConnectListNodes(pNode3, pNode4);    ConnectListNodes(pNode4, pNode5);printf("expected result: 4.\n");ListNode* pNode = FindKthToTail(pNode1, 2);PrintListNode(pNode);DestroyList(pNode1);}// 测试要找的结点是链表的尾结点void Test2(){ printf("=====Test2 starts:=====\n");    ListNode* pNode1 = CreateListNode(1);    ListNode* pNode2 = CreateListNode(2);    ListNode* pNode3 = CreateListNode(3);    ListNode* pNode4 = CreateListNode(4);    ListNode* pNode5 = CreateListNode(5);    ConnectListNodes(pNode1, pNode2);    ConnectListNodes(pNode2, pNode3);    ConnectListNodes(pNode3, pNode4);    ConnectListNodes(pNode4, pNode5);    printf("expected result: 5.\n");ListNode* pNode = FindKthToTail(pNode1, 1);PrintListNode(pNode);    DestroyList(pNode1);}// 测试要找的结点是链表的头结点void Test3(){printf("=====Test3 starts:=====\n");    ListNode* pNode1 = CreateListNode(1);    ListNode* pNode2 = CreateListNode(2);    ListNode* pNode3 = CreateListNode(3);    ListNode* pNode4 = CreateListNode(4);    ListNode* pNode5 = CreateListNode(5);    ConnectListNodes(pNode1, pNode2);    ConnectListNodes(pNode2, pNode3);    ConnectListNodes(pNode3, pNode4);    ConnectListNodes(pNode4, pNode5);    printf("expected result: 1.\n");    ListNode* pNode = FindKthToTail(pNode1, 5);    PrintListNode(pNode);    DestroyList(pNode1);}// 测试空链表void Test4(){printf("=====Test4 starts:=====\n");    printf("expected result: NULL.\n");    ListNode* pNode = FindKthToTail(NULL, 100);    PrintListNode(pNode);}// 测试输入的第二个参数大于链表的结点总数void Test5(){printf("=====Test5 starts:=====\n");    ListNode* pNode1 = CreateListNode(1);    ListNode* pNode2 = CreateListNode(2);    ListNode* pNode3 = CreateListNode(3);    ListNode* pNode4 = CreateListNode(4);    ListNode* pNode5 = CreateListNode(5);    ConnectListNodes(pNode1, pNode2);    ConnectListNodes(pNode2, pNode3);    ConnectListNodes(pNode3, pNode4);    ConnectListNodes(pNode4, pNode5);    printf("expected result: NULL.\n");    ListNode* pNode = FindKthToTail(pNode1, 6);    PrintListNode(pNode);    DestroyList(pNode1);}// 测试输入的第二个参数为0void Test6(){    printf("=====Test6 starts:=====\n");    ListNode* pNode1 = CreateListNode(1);    ListNode* pNode2 = CreateListNode(2);    ListNode* pNode3 = CreateListNode(3);    ListNode* pNode4 = CreateListNode(4);    ListNode* pNode5 = CreateListNode(5);    ConnectListNodes(pNode1, pNode2);    ConnectListNodes(pNode2, pNode3);    ConnectListNodes(pNode3, pNode4);    ConnectListNodes(pNode4, pNode5);    printf("expected result: NULL.\n");    ListNode* pNode = FindKthToTail(pNode1, 0);    PrintListNode(pNode);    DestroyList(pNode1);}int _tmain(int argc, _TCHAR *argv[]){Test1();Test2();Test3();Test4();Test5();Test6();return 0;}<pre name="code" class="cpp">struct ListNode{int m_nValue;ListNode * m_pNext;};__declspec(dllexport) ListNode *CreateListNode(int value);__declspec(dllexport) void ConnectListNodes(ListNode *pCurrent, ListNode *pNext);__declspec(dllexport) void PrintListNode(ListNode *pNode);__declspec(dllexport) void DestroyList(ListNode *pHead);
#include "List.h"#include "stdafx.h"#include <stdio.h>#include <stdlib.h>ListNode *CreateListNode(int value){ListNode *pNode = new ListNode();pNode->m_nValue = value;pNode->m_pNext = NULL;return pNode;}void ConnectListNodes(ListNode *pCurrent, ListNode *pNext){if(pCurrent == NULL){ printf("Error to connect two nodes.\n"); exit(1);// exit(1)表示异常退出.这个1是返回给操作系统的。}pCurrent->m_pNext = pNext;}void PrintListNode(ListNode *pNode){if(pNode == NULL)printf("The node is NULL\n");else printf("The key in node is %d.\n", pNode->m_nValue);}void DestroyList(ListNode *pHead){ListNode * pNode = pHead; while(pNode != NULL){pHead = pHead->m_pNext;delete pNode;pNode = pHead;}}



0 0