經典演算法——合并K個有序鏈表__演算法

來源:互聯網
上載者:User

一、題目要求:

將K個有序鏈表合并為一個有序鏈表


二、實現方法:

方法一:利用最小堆方法
用一個大小為K的最小堆(用優先隊列+自訂降序實現)(優先隊列就是大頂堆,隊頭元素最大,自訂為降序後,就變成小頂堆,隊頭元素最小),先把K個鏈表的頭結點放入堆中,每次取堆頂元素,然後將堆頂元素所在鏈表的下一個結點加入堆中。





整體測試代碼:

#include <vector>#include <iostream>#include<queue>#include<set>#include <functional> // std::greaterusing namespace std;struct ListNode{int val;ListNode* next;};struct cmp{bool operator()(ListNode* a, ListNode* b){return a->val > b->val;}};//方法一:利用最小堆方法//用一個大小為K的最小堆(用優先隊列+自訂降序實現)(優先隊列就是大頂堆,隊頭元素最大,自訂為降序後,就變成小頂堆,隊頭元素最小),//先把K個鏈表的頭結點放入堆中,每次取堆頂元素,然後將堆頂元素所在鏈表的下一個結點加入堆中。ListNode* mergeKLists2(vector<ListNode*> lists){if (lists.size() == 0) return NULL;priority_queue<int, vector<ListNode*>, cmp> heap;for (int i = 0; i < lists.size(); ++i){heap.push(lists[i]);}ListNode* newHead=NULL;ListNode* p=NULL;ListNode* q=NULL;while (!heap.empty()){q = heap.top();heap.pop();if (q->next != NULL)    heap.push(q->next);if (newHead == NULL){ newHead = q;p = q;}else{p->next = q;p = p->next;}}return newHead;}ListNode* CreateListNode(int value){ListNode* pNode = new ListNode();pNode->val = value;pNode->next = NULL;return pNode;}void DestroyList(ListNode* pHead){ListNode* pNode = pHead;while (pNode != NULL){pHead = pHead->next;delete pNode;pNode = pHead;}}void ConnectListNodes(ListNode* pCurrent, ListNode* pNext){if (pCurrent == NULL){printf("Error to connect two nodes.\n");exit(1);}pCurrent->next = pNext;}int main(){vector<ListNode*> lists;ListNode* pNode1 = CreateListNode(1);ListNode* pNode2 = CreateListNode(2);ListNode* pNode3 = CreateListNode(3);ListNode* pNode4 = CreateListNode(4);ListNode* pNode5 = CreateListNode(2);ListNode* pNode6 = CreateListNode(3);ListNode* pNode7 = CreateListNode(4);ListNode* pNode8 = CreateListNode(5);ListNode* pNode9 = CreateListNode(6);ListNode* pNode10 = CreateListNode(7);ListNode* pNode11 = CreateListNode(8);ListNode* pNode12 = CreateListNode(9);ConnectListNodes(pNode1, pNode2);ConnectListNodes(pNode2, pNode3);ConnectListNodes(pNode3, pNode4);ConnectListNodes(pNode5, pNode6);ConnectListNodes(pNode6, pNode7);ConnectListNodes(pNode7, pNode8);ConnectListNodes(pNode9, pNode10);ConnectListNodes(pNode10, pNode11);ConnectListNodes(pNode11, pNode12);ListNode* L1 = pNode1;ListNode* L2 = pNode5;ListNode* L3 = pNode9;cout << "鏈表l1: ";while (L1){cout << L1->val << " ";L1 = L1->next;}cout << endl;cout << "鏈表l2: ";while (L2){cout << L2->val << " ";L2 = L2->next;}cout << endl;cout << "鏈表l3: ";while (L3){cout << L3->val << " ";L3 = L3->next;}cout << endl;lists.push_back(pNode1);lists.push_back(pNode5);lists.push_back(pNode9);ListNode* res = mergeKLists2(lists);cout << "合并後鏈表: ";while (res){cout << res->val << " ";res = res->next;}cout << endl;system("pause");DestroyList(res);return 0;}



方法二:分治法

利用歸併排序的思想,利用遞迴和分治法將鏈表數組劃分成為越來越小的半鏈表數組,再對半鏈表數組排序,最後再用遞迴步驟將排好序的半鏈表數組合并成為越來越大的有序鏈表。





整體測試代碼:


#include <iostream>#include <vector>using namespace std;struct ListNode{int val;ListNode* next;};//方法二:分治法//利用歸併排序的思想,利用遞迴和分治法將鏈表數組劃分成為越來越小的半鏈表數組,//再對半鏈表數組排序,最後再用遞迴步驟將排好序的半鏈表數組合并成為越來越大的有序鏈表ListNode* mergeKLists(vector<ListNode*> lists, int K){return mergeLists(lists, 0, K);}ListNode* mergeLists(vector<ListNode*> listNodes, int low, int high){if (low == high) return NULL;if (high - low == 1) return listNodes[low];if (high - low == 2) return merge2(listNodes[low], listNodes[high - 1]);int mid = (high + low) / 2;ListNode* a = mergeLists(listNodes, low, mid);ListNode* b = mergeLists(listNodes, mid, high);return merge2(a, b);}ListNode* merge2(ListNode* L1, ListNode* L2){if (L1 == NULL && L2 == NULL) return NULL;else if (L1 == NULL) return L2;else if (L2 == NULL) return L1;ListNode*  newHead = NULL;ListNode* p = NULL;if (L1->val < L2->val){ newHead = L1; p = L1; L1 = L1->next; }else{ newHead = L2; p = L2; L2 = L2->next; }while (L1 != NULL && L2 != NULL){if (L1->val < L2->val){p->next = L1;L1 = L1->next;}else{p->next = L2;L2 = L2->next;}p = p->next;}p->next = L1 ? L1 : L2;return newHead;}ListNode* CreateListNode(int value){ListNode* pNode = new ListNode();pNode->val = value;pNode->next = NULL;return pNode;}void DestroyList(ListNode* pHead){ListNode* pNode = pHead;while (pNode != NULL){pHead = pHead->next;delete pNode;pNode = pHead;}}void ConnectListNodes(ListNode* pCurrent, ListNode* pNext){if (pCurrent == NULL){printf("Error to connect two nodes.\n");exit(1);}pCurrent->next = pNext;}int main(){vector<ListNode*> lists;ListNode* pNode1 = CreateListNode(1);ListNode* pNode2 = CreateListNode(2);ListNode* pNode3 = CreateListNode(3);ListNode* pNode4 = CreateListNode(4);ListNode* pNode5 = CreateListNode(2);ListNode* pNode6 = CreateListNode(3);ListNode* pNode7 = CreateListNode(4);ListNode* pNode8 = CreateListNode(5);ListNode* pNode9 = CreateListNode(6);ListNode* pNode10 = CreateListNode(7);ListNode* pNode11 = CreateListNode(8);ListNode* pNode12 = CreateListNode(9);ConnectListNodes(pNode1, pNode2);ConnectListNodes(pNode2, pNode3);ConnectListNodes(pNode3, pNode4);ConnectListNodes(pNode5, pNode6);ConnectListNodes(pNode6, pNode7);ConnectListNodes(pNode7, pNode8);ConnectListNodes(pNode9, pNode10);ConnectListNodes(pNode10, pNode11);ConnectListNodes(pNode11, pNode12);ListNode* L1 = pNode1;ListNode* L2 = pNode5;ListNode* L3 = pNode9;cout << "鏈表l1: ";while (L1){cout << L1->val << " ";L1 = L1->next;}cout << endl;cout << "鏈表l2: ";while (L2){cout << L2->val << " ";L2 = L2->next;}cout << endl;cout << "鏈表l3: ";while (L3){cout << L3->val << " ";L3 = L3->next;}cout << endl;lists.push_back(pNode1);lists.push_back(pNode5);lists.push_back(pNode9);ListNode* res = mergeKLists(lists, 3);cout << "合并後鏈表: ";while (res){cout << res->val << " ";res = res->next;}cout << endl;system("pause");DestroyList(res);return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.