LeetCode23 Merge k Sorted Lists connects K ordered linked Lists into one, leetcode23sorted

Source: Internet
Author: User

LeetCode23 Merge k Sorted Lists connects K ordered linked Lists into one, leetcode23sorted

Question:

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

Translation:

Combine K ordered linked lists and return them.

Ideas:

This question is similar to yesterday's Merge 2 Lists. But k is not sure. If we use every traversal, it will certainly not pass.

So, you can think of the merge method.

I still remember the merge assignment that the team lead arranged at the beginning of freshman year. At that time, I was just getting started, and the cloud was full of fog.

I would like to take this opportunity to make a good review.

Let's talk about the solution to this question:

Because it is a linked List, you can divide it into two groups by sub-governance, and then perform Merge 2 Lists on the two groups.

Then there are four groups. Because two chains have been formed in the previous step, you can use Merge 2Lists to recursively obtain the result.

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;    }

Okay, the code for this question is above.


Here, we need to write the merge statement. Because it will be used later.

Wiki interpretation of Merge Sorting

My personal understanding is to break down a long part into a small part, that is, divide the Sub-rule into two groups, and sort each group in four groups.

Finally, we get the overall ordered arrangement.

Baidu has an image, which is probably helpful for understanding.


Wiki Interpretation:

The working principle of Merge Sorting is as follows (assuming that the sequence has n elements ):

  1. Merge two adjacent numbers to form a sequence. After sorting, each sequence contains two elements.
  2. Merge the above sequence to form a sequence. Each sequence contains four elements.
  3. Repeat Step 2 until all elements are sorted
So, write a merge sort by 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); // divided into groups <span style =" white-space: pre "> </span> MergeSort (arr, mid + 1, r); <span style = "white-space: pre"> </span> Merge (arr, l, mid, r ); // group sorting <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]; <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 ("no." + (++ number) + "\ t"); <span style = "white-space: pre "> </span> while (ll <= r) <span style =" white-space: pre "> </span> {<span style =" white-space: pre "> </span> 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> // method stub automatically generated by TODO <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,. 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" ></span> }}
Output result:



The output clearly shows the effect of sorting After grouping.

After the statement is submitted, we can see that a group of people use C to write data in dozens of milliseconds. Sure enough!

Will study their writing. Then modify it.


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.