"Sword refers to offer study" "Face question 17:: Merge two sorted list"

Source: Internet
Author: User

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

The linked list node is defined as follows:

publicstaticclass ListNode {    int value;    ListNode next;}

Problem Solving Ideas:

See Code comments

Code implementation:

 Public  class Test17 {     Public Static  class listnode {        intValue    ListNode Next; }/** * Enter two ascending sorted lists, merge the two linked lists and make the nodes in the new list still in ascending order * * @param head1 The first ordered list * @param head2 Second ordered linked list * @return The combined ordered list header * /     Public StaticListNodeMerge(ListNode head1, ListNode head2) {//If the first linked list is empty, the second linked header node is returned.        if(Head1 = =NULL) {returnhead2; }//If the second node is empty, the first linked header node is returned .        if(Head2 = =NULL) {returnHead1; }//Create a temporary node that is convenient for adding elements.ListNode root =NewListNode ();//used to point to the trailing node of the merged new chainListNode pointer = root;//merge operation when two linked lists are not empty         while(Head1! =NULL&& head2! =NULL) {//The following actions combine smaller elements            if(Head1.value < Head2.value)                {pointer.next = Head1;            Head1 = Head1.next; }Else{pointer.next = head2;            Head2 = Head2.next; }//Move the pointer to the end of the merged listpointer = Pointer.next; }//The following two if have and only one if the content inside will be executed        //If the elements of the first list have not been processed, they will be connected to the last node of the merged list        if(Head1! =NULL) {pointer.next = Head1; }//If the elements of the second list have not been processed, they will be received after the last node of the merged list        if(Head2! =NULL) {pointer.next = head2; }//Return processing results        returnRoot.next; }/** * Enter two ascending sorted lists, merge the two linked lists and make the nodes in the new list still in ascending order * "Using recursive solution, not recommended, recursive call will have method into the stack, need more memory" * * @param  Head1 first ordered linked list * @param head2 Second ordered linked list * @return The combined ordered list head */     Public StaticListNodeMerge2(ListNode head1, ListNode head2) {//If the first linked list is empty, the second linked header node is returned.        if(Head1 = =NULL) {returnhead2; }//If the second linked list is empty, the first linked header node is returned.        if(Head2 = =NULL) {returnHead1; }//Record two smaller nodes in a linked listListNode tmp = HEAD1;if(Tmp.value < Head2.value) {//If the head node of the first list is small, recursively processes the next node of the first list and the head node of the second listTmp.next = Merge2 (Head1.next, head2); }Else{//If the head node of the second list is small, the next node of the head node of the first linked list and the head node of the second list are recursively processed.TMP = HEAD2;        Tmp.next = Merge2 (Head1, Head2.next); }//Return processing results        returntmp }/** * The element value of the output list * * @param head node of the head list */     Public Static void printlist(ListNode head) { while(Head! =NULL) {System.out.print (Head.value +" ,");        head = Head.next; } System.out.println ("NULL"); } Public Static void Main(string[] args) {ListNode head =NewListNode (); Head.value =1; Head.next =NewListNode (); Head.next.value =2; Head.next.next =NewListNode (); Head.next.next.value =3; Head.next.next.next =NewListNode (); Head.next.next.next.value =4; Head.next.next.next.next =NewListNode (); Head.next.next.next.next.value =5; ListNode head2 =NewListNode (); Head2.value =1; Head2.next =NewListNode (); Head2.next.value =3; Head2.next.next =NewListNode (); Head2.next.next.value =5; Head2.next.next.next =NewListNode (); Head2.next.next.next.value =6; Head2.next.next.next.next =NewListNode (); Head2.next.next.next.next.value =7;//head = Merge (head, head2);Head = Merge2 (head, head2);    Printlist (head); }

Operation Result:

"Sword refers to offer study" "Face question 17:: Merge two sorted list"

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.