LeetCode23 Merge k Sorted Lists connects K ordered linked lists into one

Source: Internet
Author: User

title :

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

translation :

The K-ordered list is synthesized and returned.

Ideas :

This problem is similar to yesterday's Merge 2 lists. But K is not sure, if you use each to go through, it certainly will not pass.

So, can think of is the merging method.

I also remember the first freshman group arranged the merger of the work, at that time just get started, see the foggy.

Just take this opportunity to merge a good review.

Let's talk about the solution to this problem first:

Because it is a list of lists, you can divide it into 2 groups, then merge 2Lists for each group of 2.

Then in 4 groups, because the previous step has formed 2 chains, so this can also be done through the merge 2Lists, recursion to get results.

Code :

     Public ListNode mergeklists (listnode[] lists) {  if (lists = = NULL | | lists.length = = 0)  return null;  Return mergesort (lists, 0, lists.length-1);   }  public static ListNode MergeSort (listnode[] lists,int l, int r)  {  if (l>=r)  return lists[l];  int m = (l+r)/2;  ListNode ll = mergesort (lists, L, m);  ListNode rr = mergesort (lists, m+1, R);  Return Merge (LL,RR);      }  public static ListNode Merge (ListNode l1,listnode L2)  {  ListNode root =new listnode (0);  ListNode pre = root;  Pre.next = L1;  while (true)  {  if (L1 = = null)  {  pre.next = L2;  break;  }  if (L2 = = null)  {  pre.next = L1;  break;  }  if (L1.val < L2.val)  {  pre.next = L1;  L1 = L1.next;  }  else  {  pre.next = L2;  L2 = L2.next;   }  Pre = Pre.next;    }  return root.next;    }

Well, the code for this problem is above.


Here, we still have to write a merger. Because after all, it will be used later.

Merge sort Wiki Explanation

The personal understanding is to divide a long into a small part, that is, divide and conquer, divided into 2 groups, sorting each group after it is done in 4 groups.

Finally, the whole ordered arrangement is obtained.

A picture of Baidu, estimated to be helpful to understanding


Explanation of Wiki :

Merge sort works as follows (assuming that the sequence has n elements):

    1. Merges each contiguous two digits of a sequence, forming a sequence that contains two elements for each sequence after sorting
    2. Merge the above sequence again, forming a sequence with four elements per sequence
    3. Repeat step 2 until all elements are sorted
So, write a merge sort yourself.

public class MergeSort {<span style= "white-space:pre" ></span><span style= "White-space:pre" ></ span>static int Number=0;<span style= "white-space:pre" ></span>public static void MergeSort (int arr[], int l, int r) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>if ( L >= R) <span style= "White-space:pre" ></span>return; <span style= "White-space:pre" ></span >int mid = (l+r)/2;<span style= "White-space:pre" ></span>mergesort (arr, L, mid);//split into groups <span style= " White-space:pre "></span>mergesort (arr, mid+1, R); <span style=" White-space:pre "></span>Merge ( Arr, L, Mid, R);//Intra-group sort <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></ Span>public static void Merge (int arr[],int l,int Mid, int r) <span style= "White-space:pre" ></span>{< Span style= "White-space:pre" ></span>int []tep = new INT[ARR.LENGTH];&LT;SPAn style= "White-space:pre" ></span>int mm = Mid+1;<span style= "White-space:pre" ></span>int count = L,ll = L;<span style= "White-space:pre" ></span>while (L <= mid && mm <= R) <span style= "White -space:pre "></span>{<span style=" White-space:pre "></span>if (Arr[l]  <= arr[mm]) < Span style= "White-space:pre" ></span>tep[count++]  = arr[l++];<span style= "White-space:pre" > </span>else<span style= "White-space:pre" ></span>tep[count++] = Arr[mm++];<span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>while (l<= mid) <span Style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>tep[count++] = arr[l++] ; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>while (mm < =R) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>tep[count++] = Arr[mm++];<span style= "white-space:pre" ></span>}<span style= "White-space:pre" ></span> System.out.println ("++number" + "trip \ T") <span style= "White-space:pre" ></span> while (ll<=r) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span >   &NBSP;ARR[LL] = Tep[ll];<span style= "White-space:pre" ></span>    system.out.print ( ARR[LL] + "\ t"); <span style= "White-space:pre" ></span>    ll++;<span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>    system.out.println (); <span Style= "White-space:pre" ></span><span style= "White-space:pre" ></span>}<span style= " White-space:pre "></span>/**<span style=" White-space:pre "></span> * @param args<span style=" White-space:pre "></span> */<span style=" White-space:pre "></span>public static void MaIn (string[] args) {<span style= "white-space:pre" ></span>//TODO Auto-generated method stub <span style= "White-space:pre "></span>int[] A = {9,20,32,12,53,23,54,2};<span style=" White-space:pre "></span>     MergeSort (A, 0, a.length-1); <span style= "White-space:pre" ></span>for (int t:a) <span style= " White-space:pre "></span>{<span style=" White-space:pre "></span>system.out.println (t);< Span style= "White-space:pre" ></span>}<span style= "White-space:pre" &GT;&LT;/SPAN&GT;}}
Output Result:



The effect of sorting after grouping can be clearly seen in the output.

After the submission, I saw a group of people write in C only more than 10 milliseconds passed. Sure enough, Dick!

We'll study how they are written. And then in the modification.


LeetCode23 Merge k Sorted Lists connects K ordered linked lists into one

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.