剑指offer --链表
来源:互联网 发布:韩国人的生活水平知乎 编辑:程序博客网 时间:2024/05/16 14:37
#include <iostream>#include <stack>#include <stdio.h>using namespace std;struct Node{ int value; Node* next; Node(int v):value(v){}};void InsertList(Node *head,int key){ Node* NewNode = new Node(key); NewNode->next=nullptr; if(head==nullptr) { head=NewNode; } else { Node *pNode=head; while(pNode->next!=nullptr) { pNode=pNode->next; } pNode->next=NewNode; }}void PrintList(Node *head){ Node *pNode=head; if(pNode==nullptr) printf("empity list!!"); else { while(pNode->next!=nullptr) { printf("%d->",pNode->value); pNode=pNode->next; } printf("%d\n",pNode->value); }}void PrintListReversingly(Node *head){ stack<Node*> nodes; Node *pNode=head; if(pNode==nullptr) { printf("empty list!!!"); } else { while(pNode->next!=nullptr) { nodes.push(pNode); pNode=pNode->next; } nodes.push(pNode); } while(!nodes.empty()) { pNode=nodes.top(); printf("%d->",pNode->value); nodes.pop(); } printf("\n");}Node* FindKthToTail(Node *head,int k){ if(head==nullptr||k==0) { return nullptr; } Node* pAhead = head; Node* pBhead = nullptr; for(int i=0;i<k-1;i++) { if(pAhead->next!=nullptr) { pAhead=pAhead->next; } else return nullptr; } pBhead=head; while(pAhead->next!=nullptr) { pAhead=pAhead->next; pBhead=pBhead->next; } return pBhead;}int main(){ Node *head1=nullptr; head1 = (Node *)malloc(sizeof(Node)); InsertList(head1,1); InsertList(head1,2); InsertList(head1,3); InsertList(head1,3); InsertList(head1,3); InsertList(head1,3); PrintList(head1); PrintListReversingly(head1); Node *target = FindKthToTail(head1,5); printf("%d\n",target->value);}
0 0
- 剑指offer 链表
- 剑指offer-链表
- (剑指offer)链表
- 剑指offer --链表
- 剑指offer 链表
- 剑指Offer Java版 链表
- [剑指offer]算法3 链表
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- RecyclerView 上下左右四种间距的设置方法
- spring使用xml方式配置属性值
- Java多线程学习(吐血超详细总结)
- udp协议
- svn——“验证位置时发生错误” e170001: 认证失败
- 剑指offer --链表
- Codeforces 4A Watermelon
- 数组array
- eclipse连接数据库时:“用户XX 登录失败”
- 读《图解TCPIP》
- HTTP状态码
- 进一步认识golang中的并发
- Mac快捷键总结
- [RK3288][Android6.0] 调试笔记 --- I2C通信失败问题