链表的倒数第k个节点
来源:互联网 发布:软件本地化 编辑:程序博客网 时间:2024/05/22 02:04
题目:
求链表的倒数第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;}
运行结果:
0 0
- 链表倒数第k个节点
- 链表倒数第k个节点
- 链表倒数第k个节点
- 链表倒数第K个节点
- 链表倒数第k个节点
- 链表倒数第k个节点
- 链表倒数第K个节点
- 输出链表的倒数第k个节点
- 寻找链表的倒数第k个节点
- 寻找单向链表的倒数第k个节点
- 链表的倒数第k个节点
- 输出链表的倒数第k个节点
- 寻找链表的倒数第K个节点
- 输出链表的倒数第k个节点
- 单向链表的倒数第k个节点
- 输出单向链表倒数第k个节点,倒数第0个指向尾节点.
- 求链表的倒数第K个节点
- 求链表的倒数第K个节点
- android httpClient 支持HTTPS的2种处理方式
- Vim命令合集
- 收集的48个Shell脚本小技巧
- Crontab的格式
- Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
- 链表的倒数第k个节点
- Struts(16)异常处理
- Android/Java 读取txt,转二维数组
- 大神博客
- BCB Timer定时时间小于执行时间
- vector的成员函数解析
- HTML5 Web存储(Web Storage)技术及用法
- 关于AndroidStudio导入.so文件笔记
- C++_TCHAR字符串在UNICODE环境的控制台下输出