剑指offer 17---合并两个排序的链表

来源:互联网 发布:linux服务器端口开放 编辑:程序博客网 时间:2024/05/16 12:59

一.链表1:1 3 5 7 9    链表2:2 4 6 8 10      升序排列

#include <iostream>#include <Windows.h>using namespace std;//合并两个有序链表,假设链表是升序排列//链表1:1,3,5,7,9      链表2:2,4,6,8,10struct ListNode{int _value;ListNode* _next;ListNode(const int& value):_value(value), _next(NULL){}};ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    //链表合并{if (pHead1 == NULL){return pHead2;}else if (pHead2 == NULL){return pHead1;}ListNode* MergeHead = NULL;if (pHead1->_value < pHead2->_value){MergeHead = pHead1;MergeHead->_next = Merge(pHead1->_next, pHead2);}else{MergeHead = pHead2;MergeHead->_next = Merge(pHead1, pHead2->_next);}return MergeHead;}int main(){ListNode* pHead1 = new ListNode(1);ListNode* cur1 = pHead1;for (int i = 3; i < 10; i = i + 2){ListNode* temp1 = new ListNode(i);cur1->_next = temp1;cur1 = temp1;}ListNode* pHead2 = new ListNode(2);ListNode* cur2 = pHead2;for (int j = 4; j <= 10; j = j + 2){ListNode* temp2 = new ListNode(j);cur2->_next = temp2;cur2 = temp2;}ListNode* MergeNode = Merge(pHead1, pHead2);cout << "合并后的链表为:" << "";while (MergeNode != NULL){cout << MergeNode->_value << "->";MergeNode = MergeNode->_next;}cout << endl;system("pause");return 0;}




二.对没有规律的有序链表合并

链表1:12 16 18 20 89 67 78 81 99   链表2: 2 4 66 89 99 108 10000
 

#include <iostream>#include <Windows.h>using namespace std;//合并两个有序链表,假设链表是升序排列//链表1:12 16 18 20 89 67 78 81 99   链表2: 2 4 66 89 99 108 10000struct ListNode{int _value;ListNode* _next;ListNode(const int& value):_value(value), _next(NULL){}};ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    //链表合并{if (pHead1 == NULL){return pHead2;}else if (pHead2 == NULL){return pHead1;}ListNode* MergeHead = NULL;if (pHead1->_value < pHead2->_value){MergeHead = pHead1;MergeHead->_next = Merge(pHead1->_next, pHead2);}else{MergeHead = pHead2;MergeHead->_next = Merge(pHead1, pHead2->_next);}return MergeHead;}int main(){//链表1:12 16 18 20 33 67 78 81 99   链表2: 2 3 4 66 89 99 108 10000ListNode* pHead1 = new ListNode(12);ListNode* cur1 = pHead1;ListNode* temp1 = new ListNode(16);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(18);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(20);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(33);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(67);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(78);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(81);cur1->_next = temp1;cur1 = temp1;temp1 = new ListNode(99);cur1->_next = temp1;cur1 = temp1;//2 4 66 89 99 108 10000ListNode* pHead2 = new ListNode(2);ListNode* cur2 = pHead2;ListNode* temp2 = new ListNode(3);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(4);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(66);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(89);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(99);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(108);cur2->_next = temp2;cur2 = temp2;temp2 = new ListNode(10000);cur2->_next = temp2;cur2 = temp2;ListNode* MergeNode = Merge(pHead1, pHead2);cout << "合并后的链表为:" << "";while (MergeNode != NULL){cout << MergeNode->_value << "->";MergeNode = MergeNode->_next;}cout << endl;system("pause");return 0;}


原创粉丝点击