[Leetcode] [JAVA] Merge Sorted Lists & Sort List

Source: Internet
Author: User

Merge Sorted Lists:

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.

Two sequential list concatenation, as long as the two pointers to the list can be traversed. You can use a helper pointer to start a merge. If one of the loops is complete, the rest of the following list pointers are immediately followed:

1  PublicListNode mergetwolists (listnode L1, ListNode L2) {2ListNode helper =NewListNode (0);3ListNode Cur1 =L1;4ListNode CUR2 =L2;5ListNode cur =Helper;6          while(cur1!=NULL|| cur2!=NULL)7         {8             if(cur1==NULL)9             {TenCur.next =Cur2; One                  Break; A             } -             if(cur2==NULL) -             { theCur.next =Cur1; -                  Break; -             } -             if(cur1.val<=cur2.val) +             { -Cur.next =Cur1; +cur1=Cur1.next; A             } at             Else -             { -Cur.next =Cur2; -Cur2=Cur2.next; -             } -Cur=Cur.next; in         } -         returnHelper.next; to}

Sort List:

Sort a linked list in O(n log n) time using constant space complexity.

With merged two Sorted lists Foundation, this question is very good to write. Referring to O (nlogn) sorting algorithm, immediately think of fast and merge sort, but the fast row is not stable and for the linked list exchange inconvenient, so decided to use the merge sort.

For linked lists, the split process in merge sort can be implemented using a fast and slow pointer, which is straightforward:

1  PublicListNode sortlist (ListNode head) {2         returnMergeSort (head);3     }4     5      Publiclistnode mergesort (listnode start)6     {7         if(start==NULL)return NULL;8         if(start.next==NULL)returnstart;9         TenListNode slow =start; OneListNode fast =start; A          while(fast!=NULL&& fast.next!=NULL&& fast.next.next!=NULL) -         { -slow =Slow.next; theFast =Fast.next.next; -         } -ListNode second =Slow.next; -Slow.next =NULL; +         returnmergetwolists (MergeSort (start), MergeSort (second)); -     } +      PublicListNode mergetwolists (listnode L1, ListNode L2) { AListNode helper =NewListNode (0); atListNode Cur1 =L1; -ListNode CUR2 =L2; -ListNode cur =Helper; -          while(cur1!=NULL|| cur2!=NULL) -         { -             if(cur1==NULL) in             { -Cur.next =Cur2; to                  Break; +             } -             if(cur2==NULL) the             { *Cur.next =Cur1; $                  Break;Panax Notoginseng             } -             if(cur1.val<=cur2.val) the             { +Cur.next =Cur1; Acur1=Cur1.next; the             } +             Else -             { $Cur.next =Cur2; $Cur2=Cur2.next; -             } -Cur=Cur.next; the         } -         returnHelper.next;Wuyi}

[Leetcode] [JAVA] Merge Sorted Lists & Sort List

Related Article

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.