Merge two sorted lists

Source: Internet
Author: User

Title: Input two monotonically increasing list, output two linked list of the linked list, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.

Idea: Sweep the maintenance increment again, and finally add the parts of the original linked list that may not be added

  PublicListNode Merge (listnode list1,listnode list2) {if(list1==NULL&&list2==NULL)return NULL; if(list1==NULL&&list2!=NULL)returnList2; if(list1!=NULL&&list2==NULL)returnList1; ListNode res1=NewListNode (-1); ListNode Res=res1; //to have two pointers pointing to the created head node, a pointer will go to the end and the return time is no longer available.         while(list1!=NULL&&list2!=NULL){            if(list1.val<=list2.val) {Res.next=List1; Res=Res.next; List1=List1.next; }Else{Res.next=List2; Res=Res.next; List2=List2.next; }        }          while(List1! =NULL) {Res.next=List1; Res=Res.next; List1=List1.next; }         while(List2! =NULL) {Res.next=List2; Res=Res.next; List2=List2.next; }        returnRes1.next; }

Merge 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.