/ * Title: Enter the two ascending order of the list, merge the two lists and the list of the new node is still ascending order. Returns the new head node list. * / / * Ideas: 1, the head node returns the list is the list head node two smaller values is a linked list. 2, compares 3, node 2 determines whether the list is empty, if not empty then all added to the tail of the list 1. * / #Include <the iostream> #include <the cstdlib> the using namespace STD; struct ListNode { int Val; struct * Next ListNode; ListNode (int X): Val (X), Next (NULL) { } }; ListNode the Merge * ( * pHead1 ListNode, ListNode pHead2 *) { IF (pHead1 == nullptr a) return pHead2; IF (pHead2 == nullptr a) return pHead1; ListNode nullptr a = * • pNode; ListNode qNode = nullptr a *; ListNode nullptr a * = TEMP; ListNode* head = nullptr; if(pHead1->val < pHead2->val){ pNode = pHead1; qNode = pHead2; head = pHead1; }else{ pNode = pHead2; qNode = pHead1; head = pHead2; } while(pNode->next != nullptr && qNode != nullptr){ if(pNode->next->val <= qNode->val){ pNode = pNode->next; }else{ temp = pNode->next; pNode->next = qNode; qNode = qNode->next; pNode->next->next = temp; } } if(pNode->next == nullptr){ pNode->next = qNode; } return head; } int main() { ListNode *node6 = new ListNode(6); ListNode *node5 = new ListNode(5); ListNode *node4 = new ListNode(4); ListNode *pHead2 = new ListNode(1); ListNode *node3 = new ListNode(5); ListNode *node2 = new ListNode(2); ListNode *node1 = new ListNode(2); ListNode *pHead1 = new ListNode(0); pHead1->next = node1; node1->next = node2; node2->next = node3; node3->next = nullptr; pHead2->next = node4; node4->next = node5; node5->next = node6; node6->next = nullptr; ListNode* pHead = Merge(pHead1,pHead2); cout<<"answer"<<endl; while(pHead != nullptr){ cout<<pHead->val<<endl; pHead = pHead->next; } cout << "Hello world!" << endl; return 0; }