面试题15:合并两个排序的链表 (二)

2015-11-21 01:27:46 · 作者: · 浏览: 23
d) { pHead = new ListNode(); cin >> pHead->m_nValue; if (pHead->m_nValue == -1) { pHead = NULL; break; } pHead->m_pNext = NULL; isHead = false; pCurLastNode = pHead; } else { pListNode = new ListNode(); cin >> pListNode->m_nValue; if (pListNode->m_nValue == -1) { break; } pListNode->m_pNext = NULL; pCurLastNode->m_pNext = pListNode; pCurLastNode = pListNode; } } } //从头到尾打印链表 void PrintList(ListNode *&pHead) { if (pHead != NULL) { ListNode *pCur = pHead; while (pCur != NULL) { cout << pCur->
m_nValue << " "; pCur = pCur->m_pNext; } cout << endl; } else { cout << "链表为空!" << endl; } } int _tmain(int argc, _TCHAR* argv[]) { ListNode *pList1Head = NULL; CreateList(pList1Head); PrintList(pList1Head); ListNode *pList2Head = NULL; CreateList(pList2Head); PrintList(pList2Head); ListNode *pMergeListHead = MergeTwoList(pList1Head, pList2Head); if (pMergeListHead != NULL) { cout << pMergeListHead->m_nValue << endl; } PrintList(pMergeListHead); system("pause"); return 0; }

?