题目:
求链表的倒数第k个节点
题目要求:
1 输入一个单向链表,输出该链表中倒数第k个结点,
2 链表的倒数第0个结点为链表的尾指针(即与倒数第1个节点)
3 链表为空或者链表结点数小于k,返回空
题目解析:
设置两个指针pre,node; 将pre,node都指向链表第一个节点,然后node向前走pre步,这样pre和node之间就相隔k个节点,然后pre,node同时移动直到node指向链表末尾。此时pre位链表的倒数第k个节点
注意事项:
1 链表是否为空
2 链表节点数小于k
算法实现代码:
//-------------链表、链表结点、链表简单操作定义---------------------
struct Node
{
char c;
Node *next;
};
//链表有头结点,list->head->next指向第一个节点
struct List
{
Node * head;
};
//返回list的第k个节点
Node* ListBack_K_Node(List *list, unsigned k)
{
//如果链表为空,则返回结果为空
if (nullptr == list || nullptr == list->head)
{
return nullptr;
}
//如果k=0;返回k = 1时的结果
if (k == 0)
{
k++;
}
//node向前走k步
Node*node = list->head->next;
while ((unsigned)0 != k && nullptr != node)
{
--k;
node = node->next;
}
//链表结点数小于k,则直接返回空
if (unsigned(0) != k)
{
return nullptr;
}
//pre,node同步走,直到node == nullptr,此时pre即为倒数第k个节点
Node *pre = list->head->next;
while (nullptr != node)
{
node = node->next;
pre = pre->next;
}
return pre;
}
链表实现、算法测试代码:
#include <iostream>
#include <string>
using namespace std;
//-------------链表、链表结点、链表简单操作定义---------------------
struct Node
{
char c;
Node *next;
};
//链表有头结点,list->head->next指向第一个节点
struct List
{
Node * head;
};
Node* AllocNode()
{
Node *node = new Node;
if (nullptr == node)
{
return nullptr;
}
node->next = nullptr;
return node;
}
void InitList(List *list)
{
list->head = AllocNode();
}
//在链表头部插入一个结点
Node* AddNodeToList(List *list, char c)
{
//如果链表为空,直接返回
if (nullptr == list)
{
return nullptr;
}
//如果链表头结点为空,则对链表初始化
if (nullptr == list->head)
{
InitList(list);
}
Node *node = AllocNode();
node->c = c;
node->next = list->head->next;
list->head->next = node;
return list->head->next;
}
//根据str创建链表
Node* CreateList(List *list, const string &str)
{
if (nullptr == list)
{
return nullptr;
}
for (size_t i = 0; i < str.size(); ++i)
{
AddNodeToList(list, str[i]);
}
return list->head->next;
}
//销毁链表
void DestroyList(List *list)
{
if (nullptr == list || nullptr == list->head)
{
return;
}
Node *node = nullptr;
while (nullptr != list->head->next)
{
node = list->head->next;
list->head->next = node->next;
delete node;
}
delete list->head;
list->head = nullptr;
}
void PrintList(const List* list)
{
if (nullptr == list || nullptr == list->head)
{
return;
}
for (Node *node = list->head->next; nullptr != node; node = node->next)
{
cout << node->c;
}
cout << endl;
}
//---------------------------------------------------------------------------------------------
//返回list的第k个节点
Node* ListBack_K_Node(List *list, unsigned k)
{
//如果链表为空,则返回结果为空
if (nullptr == list || nullptr == list->head)
{
return nullptr;
}
//如果k=0;返回k = 1时的结果
if (k == 0)
{
k++;
}
//node向前走k步
Node*node = list->head->next;
while ((unsigned)0 != k && nullptr != node)
{
--k;
node = node->next;
}
//链表结点数小于k,则直接返回空
if (unsigned(0) != k)
{
return nullptr;
}
//pre,node同步走,直到node == nullptr,此时pre即为倒数第k个节点
Node *pre = list->head->next;
while (nullptr != node)
{
node = node->next;
pre = pre->next;
}
return pre;
}
void TestListBack_K_Node(List *list, int k)
{
Node *node = ListBack_K_Node(list, k);
cout << "Search List's Back " << k << " node ";
if (nullptr != node)
{
cout << "success ,node.c = " << node->c << endl;
}
else
{
cout << "failure" << endl;
}
}
int main()
{
string str("123456789");
List list;
InitList(&list);
CreateList(&list, str);
PrintList(&list);
TestListBack_K_Node(&list, 0);
TestListBack_K_Node(&list, 1);
TestListBack_K_Node(&list, 5);
TestListBack_K_Node(&list, 9);
TestListBack_K_Node(&list, 10);
TestListBack_K_Node(nullptr, 0);
DestroyList(&list);
return 0;
}
运行结果: