合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)
来源:互联网 发布:mac os x10.11安装 编辑:程序博客网 时间:2024/06/03 03:42
//1.【基础题】--合并两个有序链表,合并以后的链表依旧有序。 #include<iostream>using namespace std;typedef struct Node{ int data; struct Node* next; Node(int x) :data(x) ,next(NULL) {}}Node;Node* MergeList(Node* Plist1,Node* Plist2){ if (Plist1==NULL)//如果链表1为空,返回链表2 { return Plist2; } if(Plist2==NULL)//链表2为空,返回链表1 { return Plist1; } if(Plist1==Plist2)//如果两个链表相等,返回任意一个 { return Plist1; } //两个链表都不为空 Node* cur1=Plist1;//用于遍历链表1 Node* cur2=Plist2;//用于遍历链表2 Node* NewHead=NULL;//定义合并后新链表的头结点指针 if (Plist1->data>Plist2->data) { NewHead=Plist2; cur2=cur2->next; } else { NewHead=Plist1; cur1=cur1->next; } Node* NewNode=NewHead;//用于记录新链表的尾 while (cur1&&cur2) { if (cur1->data > cur2->data) { NewNode->next=cur2; cur2=cur2->next; NewNode=NewNode->next;//更新新链表的尾结点 } else { NewNode->next=cur1; cur1=cur1->next; NewNode=NewNode->next;//更新新链表的尾结点 } } //至少有一个链表走完了 //当链表1没有走完时,将剩下的部分连接到新链表上 if (cur1) { NewNode->next=cur1; } //当链表2没有走完时,将剩下的部分连接到新链表上 if (cur2) { NewNode->next=cur2; } return NewHead;}void Printf(Node* PHead){ if (PHead==NULL) { return ; } Node* cur=PHead; while (cur) { cout<<cur->data<<" "; cur=cur->next; }}int main(){ Node Node1(1); Node Node2(3); Node Node3(5); Node Node4(7); Node Node5(9); Node* PHead1=&Node1; Node1.next=&Node2; Node2.next=&Node3; Node3.next=&Node4; Node4.next=&Node5; Node Node11(2); Node Node22(4); Node Node33(6); Node Node44(8); Node Node55(10); Node* PHead2=&Node11; Node11.next=&Node22; Node22.next=&Node33; Node33.next=&Node44; Node44.next=&Node55; Node* NewHead=MergeList(PHead1,PHead2); Printf(NewHead); return 0;}----------//2.逆置/反转单链表#include<iostream>#include<assert.h>using namespace std;typedef struct Node{ int data; struct Node* next; Node(int x) :data(x) ,next(NULL) {}}Node;Node* InverseList(Node* PHead){ //assert(PHead); Node* NewHead=NULL;//创建一个新的头结点指针 Node* cur=PHead;//遍历原链表 while (cur) { Node* tmp=cur;//记录当前位置 cur=cur->next;//cur移到下一位置 tmp->next=NewHead; NewHead=tmp;//更新头结点 } return NewHead;}void Printf(Node* PHead){ assert(PHead); Node* cur=PHead; while (cur) { cout<<cur->data<<" "; cur=cur->next; }}int main(){ Node* PHead=new Node(1); Node* Node2=new Node(2); Node* Node3=new Node(3); Node* Node4=new Node(4); Node* Node5=new Node(5); PHead->next=Node2; Node2->next=Node3; Node3->next=Node4; Node4->next=Node5; Printf(PHead); cout<<endl; PHead=InverseList(PHead); Printf(PHead); return 0;}----------//3.查找单链表的倒数第k个节点,要求只能遍历一次链表 #include<iostream>#include<assert.h>using namespace std;typedef struct Node{ int data; struct Node* next; Node(int x) :data(x) ,next(NULL) {}}Node;Node* FindKNode(Node* PHead,int n){ assert(PHead); Node* cur=PHead; Node* start=PHead; Node* end=NULL; while (--n) { cur=cur->next; } end=cur; while (end->next) { end=end->next; start=start->next; } return start;}int main(){ Node* Node1=new Node(1); Node* Node2=new Node(2); Node* Node3=new Node(3); Node* Node4=new Node(4); Node* Node5=new Node(5); Node* Node6=new Node(6); Node* Node7=new Node(7); Node* Node8=new Node(8); Node1->next=Node2; Node2->next=Node3; Node3->next=Node4; Node4->next=Node5; Node5->next=Node6; Node6->next=Node7; Node7->next=Node8; Node* Node=FindKNode(Node1,3); cout<<Node->data<<endl; return 0;}
阅读全文
0 0
- 合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)
- 逆置/反转单链表+查找单链表的倒数第k个节点,要求只能遍历一次链表
- 逆置/反转单链表+查找单链表的倒数第k个节点,要求只能遍历一次链表
- 链表的倒数第K个结点(一次遍历)
- 查找单链表的倒数第k个节点,要求只能遍历一次链表(C语言)
- C实现简单单向链表,一次遍历查找倒数第k个节点的值
- 【链表】查找链表倒数第k个结点,要求只能遍历一次
- 查找单链表的倒数第k个节点,要求只能遍历一次链表
- 查找单链表的倒数第k个节点,要求只能遍历一次链表
- 链表--查找单链表的倒数第k个节点,要求只能遍历一次链表
- 查找单链表的倒数第k个节点,要求只能遍历一次链表
- 查找单链表的倒数第k个节点,要求只能遍历一次链表
- 链表面试题(二):冒泡排序、合并两个有序链表、查找中间节点、查找倒数K个节点
- 链表中倒数第 K 个结点-反转链表
- 反转链表和查找倒数第K个节点
- 巧妙利用两个指针遍历链表——链表中倒数第k个结点
- 链表遍历一次删除倒数第N个节点
- 反转单链表+合并有序单链表+查找单链表中倒数第k个节点--20150924
- 游戏CS:GO VAC反外挂系统分析
- STM32控制编码器
- the import cannot be resolved问题
- exploit-exercise的protostar之stack0
- Java POI 读写Excel 文件简单实现
- 合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
- 从fibonacci数列对比C语言和其对应的汇编代码
- java中基于循环嵌套的使用例题经典
- 75. could not initialize proxy
- HW--Church numerals
- 用了一年时间才大致把导师的 java 代码 看懂
- PyCharm中出现No tests were found的一种解决方法及PyCharm建立测试类的官方原文及翻译
- 线性表(List)---链式存储(循环链表)