Leetcode's Sort List

Source: Internet
Author: User

1. Description of the problem

  

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

2. Translation

  

The time complexity of O (nlog N) is used to sort the list in fixed spatial complexity.

  

3. Analysis of Ideas

In our minds, we will quickly appear in various sorting algorithms: bubble sort, quick sort, simple sort, heap sort, direct insert sort, hill sort (Descending incremental sort), direct select sort, heap sort, merge sort, which can have O (n lgn) time complexity algorithm for, fast sort, heap sort, Merge sort, and the spatial complexity of the three is O (1), O (n), O (n), in theory, in order to meet the requirements of the topic we need to find out which sorting algorithm is the most appropriate sorting algorithm. Here we use the merge sort algorithm, the spatial complexity of the merge sort is O (n), that is to say the extra space of O (n) is needed to save the array information. Because it is a linked list, we only need to modify the pointer, we only need the Space complexity O (1). In order to achieve the merge sort we need to first understand the idea of merge sort, and find the middle node and merge ordered sequence,

The principle of merge sorting divides the original sequence into two sequential sequences, then merges it by merging algorithm, then it is ordered sequence. Now that we have a clear algorithm, we can begin to implement it.

Find the middle node: Because the sorted object is a linked list we have no way to one step the length of the list. In other words, there is no way to get the middle node, we can only do it by traversing the linked list. In order to get the middle node we need to set two pointers, one is to move the pointer slower one move faster, and then determine when the fast pointer moves the end of the list when the slow pointer to the middle node, so that the middle node is obtained.

Merging: It is necessary to compare the values of the linked list and then iterate through the loops.

4. Implementing the Code

Linked list node (listnode), Listnode.java

  

Package Sortlist;public class ListNode {int val; ListNode Next; ListNode (int x) {val = X;next = null;}}

Sorting and merging implementation Solution.java

Package Sortlist;public class Solution {public ListNode sortlist (ListNode head) {ListNode slow_p = head; ListNode fast_p = head;if (head = = null) {return null;} ListNode pre = Head;while (Slow_p.next! = NULL && fast_p! = NULL && Fast_p.next! = null) {pre = Slow_p;slow _p = Slow_p.next;fast_p = Fast_p.next.next;} Pre.next = null;    ListNode res = mergesort (head, slow_p); return res; }private listnode mergesort (listnode L1, ListNode L2) {if (L2 = = NULL | | l1 = L2) {return L1;} if (L1 = = null) {return L2;} ListNode slow_p = L1; ListNode fast_p = L1; ListNode pre = L1;while (Slow_p.next! = NULL && fast_p! = NULL && Fast_p.next! = null) {pre = Slow_p;slow_ p = slow_p.next;fast_p = Fast_p.next.next;} Pre.next = null; ListNode head1 = mergesort (L1, slow_p); slow_p = l2;fast_p = L2;pre = L2;while (Slow_p.next! = NULL && fast_p! = N ull && Fast_p.next! = null) {pre = Slow_p;slow_p = Slow_p.next;fast_p = Fast_p.next.next;} Pre.next = null; ListNode head2 = MergesorT (L2, slow_p); ListNode res = new ListNode (0);  ListNode cur = res;while (head1! = NULL && head2! = null) {if (Head1.val < head2.val) {Cur.next = Head1;head1 = Head1.next;} Else{cur.next = Head2;head2 = Head2.next;} cur = cur.next;} while (head1! = null) {Cur.next = Head1;head1 = Head1.next;cur = Cur.next;} while (head2! = null) {Cur.next = Head2;head2 = Head2.next;cur = Cur.next;} return res.next;}}

Testsolution.java

Package Sortlist;public class Testsolution {/** * @param args */public static void main (string[] args) {//TODO Auto-gener Ated method Stublistnode node = new ListNode (0); ListNode head = node; ListNode temphead = head;for (int index = 0;index < 10;index++) {int temp = (int) (Math.random () *100); ListNode Tempnode = new ListNode (temp); node.next = Tempnode;node = Tempnode;} System.out.println ("Value of the list before sorting"); while (head.next!=null) {head = Head.next; System.out.println (head.val);} ListNode result = new solution (). Sortlist (Temphead); System.out.println ("Value of the sorted list"); while (result.next!=null) {result = Result.next; System.out.println (Result.val);}}}

The results of the implementation are as follows:

It can be seen from the test results that the requirements of the topic have been fulfilled.

5. Find the middle node of the list is not difficult, the more difficult sub-linked list of the above, the logic to consider is more complex.

Leetcode's Sort List

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.