Leetcode #21 Merge Sorted Lists (E)

Source: Internet
Author: User

[Problem]

Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.

[Analysis]

There is nothing to say, that is, order visit and connect the two list node together.

[Solution]

 Public classSolution { PublicListNode mergetwolists (listnode L1, ListNode L2) {ListNode Mergedhead=NewListNode (0); ListNode node=Mergedhead;  while(L1! =NULL&& L2! =NULL) {                                    if(L1.val >l2.val) {Node.next=L2; Node=Node.next; L2=L2.next; } Else{Node.next=L1; Node=Node.next; L1=L1.next; }} Node.next= L1 = =NULL?l2:l1; returnMergedhead.next; }}

Leetcode #21 Merge Sorted Lists (E)

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.