C++单向链表之合并链表
2016-04-27 13:29
423 查看
代码如下
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) { if (pHead1 == NULL) { return pHead2; } else if (pHead2 == NULL) { return pHead1; } ListNode* pMergeHead = NULL; if (pHead1->m_nValue<pHead2->m_nValue) { pMergeHead = pHead1; pMergeHead->m_pNext = Merge(pHead1->m_pNext, pHead2); } else { pMergeHead = pHead2; pMergeHead->m_pNext = Merge(pHead1, pHead2->m_pNext); } return pMergeHead; } ListNode* Test(ListNode* pHead1, ListNode* pHead2) { printf("The first list is:\n"); PrintList(pHead1); printf("The second list is:\n"); PrintList(pHead2); printf("The Merged list is:\n"); ListNode*pMergeHead = Merge(pHead1, pHead2); PrintList(pMergeHead); return pMergeHead; } void Test1() { ListNode* pNode1 = CreateListNode(1); ListNode* pNode3 = CreateListNode(3); ListNode* pNode5 = CreateListNode(5); CoonnectListNode(pNode1, pNode3); CoonnectListNode(pNode3, pNode5); ListNode* pNode2 = CreateListNode(2); ListNode* pNode4 = CreateListNode(4); ListNode* pNode6 = CreateListNode(6); CoonnectListNode(pNode2, pNode4); CoonnectListNode(pNode4, pNode6); ListNode* pMergeHead = Test(pNode1, pNode2); DestroyList(pMergeHead); }
相关文章推荐
- C++内存管理
- 基本路径测试
- c++第四次作业
- INTEL C/C++ COMPILER
- C++多态基础(polymorphism)
- 【VS开发】C++ opencv Mat基础
- 最近的两个小项目,1:在Vscode里写C/C++
- c语言学习之数组指针
- C++11 新特性:模板别名
- C++使用stringstream进行数据类型转换
- C++作业4
- C语言杂谈(三)存储类别
- C++统计代码注释行数 & 有效代码行数 & 代码注释公共行 & 函数个数
- C语言杂谈(二)自增运算符++与间接访问运算符*的结合关系和应用模式
- C++杂谈(一)const限定符与const指针
- C++杂谈(二)初识vector容器与迭代器
- C语言常见错误杂谈(一)scanf()、scanf_s()与错误 C4996与解决方法
- c++ 覆盖、重载与隐藏 浅析
- 两种方法求两个数的最大公约数和最小公倍数--C语言
- 删除vector中重复元素