合并两个有序单链表
2017-07-13 15:05
381 查看
typedef struct Node { Node(const int& value) : m_value(value) , m_pNext(NULL) {} int m_value; Node* m_pNext; }Node, *pNode ; class List { public: List() : _pHead(NULL) , _pTail(NULL) {} void PushBack(const int& value) { if (_pHead == NULL) { _pHead = _pTail = _BuyNode(value); } else { Node* temp = _BuyNode(value); _pTail->m_pNext = temp; _pTail = temp; } } pNode* CombineList(List pHead1, List pHead2) { if (pHead1._pHead == NULL || pHead2._pHead == NULL) return NULL; if (pHead1._pHead == NULL) return &pHead2._pHead; if (pHead2._pHead == NULL) return &pHead1._pHead; pNode pNewHead = NULL; pNode pHead1Next = pHead1._pHead->m_pNext; pNode pHead2Next = pHead2._pHead->m_pNext; if (pHead1._pHead->m_value < pHead2._pHead->m_v 4000 alue) { pNewHead = pHead1._pHead; pHead1._pHead = pHead1Next; if (pHead1Next) pHead1Next = pHead1Next->m_pNext; pNewHead->m_pNext = NULL; } else { pNewHead = pHead2._pHead; pHead2._pHead = pHead2Next; if (pHead2Next) pHead2Next = pHead2Next->m_pNext; pNewHead->m_pNext = NULL; } pNode pNew = pNewHead; while (pHead1._pHead&&pHead2._pHead) { if (pHead1._pHead->m_value < pHead2._pHead->m_value) { pNew->m_pNext = pHead1._pHead; pHead1._pHead = pHead1Next; if (pHead1Next) pHead1Next = pHead1Next->m_pNext; } else { pNew->m_pNext = pHead2._pHead; pHead2._pHead = pHead2Next; if (pHead2Next) pHead2Next = pHead2Next->m_pNext; } pNew = pNew->m_pNext; } if (pHead1._pHead) pNew->m_pNext = pHead1._pHead; if (pHead2._pHead) pNew->m_pNext = pHead2._pHead; return &pNewHead; } private: pNode _BuyNode(const int& value) { return new Node(value); } private: pNode _pHead; pNode _pTail; };
相关文章推荐
- 合并两个有序单链表,合并后依旧有序
- 合并两个单链表为递减有序的单链表
- 输入两个数列,分别以某数为结束标志,分别生成递增有序单链表;再将这两个表合并为一个递增有序单链表,输出表中各结点的值
- 两个有序单链表的合并&两个有序数组的合并
- 两个有序单链表合并成一个有序单链表的java实现
- 微软暑期实习笔试题 合并两个有序单链表
- 9、单链表的合并,两个有序单链表,合并为一个有序链表。
- 两个有序单链表合并成一个有序单链表的java实现
- 两个无序单链合并成一个有序单链表
- 合并两个有序单链表,使得合并后的链表仍然有序
- Java 非递归 和非递归方法 实现两个有序单链表的合并
- 数据结构 — 1. 两个非递减有序单链表合并为非递增有序单链表
- 两个有序单链表的合并
- 合并两个有序单链表的递归方法
- Java单链表基本操作(八)--合并两个有序单链表
- 面试题---两个有序单链表的合并
- 合并两个有序单链表(Java)
- 非递减合并两个有序单链表
- LeetCode 21. Merge Two Sorted Lists 合并两个有序单链表 C++
- java 合并两个有序单链表