Interview Title: Merge two sorted lists

Source: Internet
Author: User

Title: Enter two incrementally sorted lists, merge the two linked lists, and make the nodes in the new list still incrementing.


For example:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/89/2F/wKiom1gKMKzCGbbCAAA1-4c1Xrg589.jpg "title=" 20161021230805.jpg "alt=" Wkiom1gkmkzcgbbcaaa1-4c1xrg589.jpg "/>

/* Merge two sorted list *//* the linked list node is defined as follows: struct Listnode{int _data; Listnode* _next;}; */listnode* mergelist (listnode* pHead1, listnode* pHead2) {if (PHead1 = = NULL)//list 1 is empty linked list {return pHead2;} else if (pHead2 = = null)//List 2 is empty linked list {return pHead1;} else//list 1, 2 are not empty list {listnode* Pmergehead = null;//recursion, treat the remaining list as a sub-problem//Find the node with the smaller value as the Pmergehead chain to the previous layer Pmergehead->_next on if ( Phead1->_data < Phead2->_data) {Pmergehead = Phead1;pmergehead->_next = Mergelist (PHead1->_next, pHead2 );} Else{pmergehead = Phead2;pmergehead->_next = Mergelist (PHead1, phead2->_next);} return pmergehead;}}


This article is from the "zgw285763054" blog, make sure to keep this source http://zgw285763054.blog.51cto.com/11591804/1864429

Interview title: Merging two sorted lists

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.