Merge two sorted linked lists

Source: Internet
Author: User

/***************************************: Enter two linked lists in ascending order, merge these two linked lists, and keep the nodes in the new linked list in ascending order. **************************************** * *************************/# Include
 
  
Struct ListNode {int m_nValue; ListNode * m_pNext;}; ListNode * createListNode (int value) {ListNode * pNode = new ListNode (); pNode-> m_nValue = value; pNode-> m_pNext = NULL; return pNode;} void connectListNode (ListNode * pListNode1, ListNode * pListNode2) {if (pListNode2 = NULL | pListNode2 = NULL) return; pListNode1-> m_pNext = pListNode2;} ListNode * mergeSortedLists (ListNode * pList1, ListNode * pList2) {if (! PList1) return pList2; if (! PList2) return pList1; ListNode * pHead; if (pList1-> m_nValue <= pList2-> m_nValue) {pHead = pList1; pHead-> m_pNext = cursor (pList1-> m_pNext, pList2);} if (pList1-> m_nValue> pList2-> m_nValue) {pHead = pList2; pHead-> m_pNext = mergeSortedLists (pList1, pList2-> m_pNext );} return pHead;} void printList (ListNode * pNode) {if (pNode = NULL) printf ("List is NULL"); while (pNode) {printf ("% d \ t", pNode-> m_nValue); pNode = pNode-> m_pNext;} printf ("\ n ");} // unit test // two non-empty linked lists void test1 () {ListNode * pList1_Node1 = createListNode (1); ListNode * pList1_Node2 = createListNode (3 ); listNode * pList1_Node3 = createListNode (5); ListNode * vertex = createListNode (7); connectListNode (connector, pList1_Node2); connectListNode (connector, connector); connectListNode (pList1_Node3, pList1_Node4 ); printList (pList1_Node1); ListNode * pList2_Node1 = createListNode (2); ListNode * pList2_Node2 = createListNode (4); ListNode * pList2_Node3 = createListNode (6 ); listNode * connector = createListNode (8); connectListNode (pList2_Node1, pList2_Node2); connectListNode (connector, connector); connectListNode (pList2_Node3, pList2_Node4); printList (connector ); listNode * pHead = mergeSortedLists (pList1_Node1, pList2_Node1); printList (pHead);} // an empty linked list and another non-empty void test2 () {ListNode * pList1_Node1 = createListNode ); listNode * nodes = createListNode (3); ListNode * pList1_Node3 = createListNode (5); ListNode * pList1_Node4 = createListNode (7); connectListNode (connector, pList1_Node2); connectListNode (pList1_Node2, pList1_Node3); connectListNode (pList1_Node3, pList1_Node4); printList (pList1_Node1); ListNode * pHead = mergeSortedLists (pList1_Node1, NULL); printList (pHead );} // two non-empty linked lists void test3 () {ListNode * pHead = mergeSortedLists (NULL, NULL); printList (pHead);} int main () {test1 (); test2 (); test3 (); return 0 ;}
 


= Reference offoffoffoffoffer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.