[leetcode#148] Sort List

Source: Internet
Author: User

The problem:

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

My Analysis:

The idea behind this problem is easy:merge sort!
But we should learn some tricky skills from the this question.
1. How to split a linked list into separate linked lists?
A. Use Pointers:walker and runner.
A.1 Walker move one step in each iteration.
A.2 runner move, steps in each iteration.
B. Iterate until runner can ' t move any more (Runner.next = = NULL | | runner.next.next = NULL)
C. Then head1 are the head of original list, and head2 is the next element of runner.

2. How to merge the linked list in an elegant?
The key idea behind writing a elegant code is always use the same invarint.
To achieve this purpose, I use a dummy head as fake head and a pre Pinter always point to the last element of the Resultin G list.
ListNode dummy = new ListNode (0);
Pre = dummy;
...
while () {
Pre.next = ...;
}
return dummy.next;

My Code:

/*** Definition for singly-linked list. * Class ListNode {* int val; * ListNode Next; * ListNode (int x) {* val = x; * next = NULL; *     } * } */ Public classSolution { PublicListNode sortlist (ListNode head) {returnMergeSort (head); }        PrivateListNode mergesort (ListNode head) {if(Head = =NULL|| Head.next = =NULL)//The base case in the recursion is very important!            returnHead; ListNode Walker=Head; ListNode Runner=Head;  while(Runner.next! =NULL&& Runner.next.next! =NULL) {//Skill:check Runner.next at first.Walker= Walker.next;//This skill is amazing!!!Runner =Runner.next.next; } ListNode head2=Walker.next; Walker.next=NULL; ListNode Head1=Head; Head1=mergesort (HEAD1); Head2=mergesort (head2); Head=merge (Head1, head2); returnHead; }        Privatelistnode Merge (ListNode head1, ListNode head2) {if(Head1 = =NULL&& Head2 = =NULL)            return NULL; if(Head1 = =NULL&& head2! =NULL)            returnhead2; if(Head1! =NULL&& Head2 = =NULL)            returnHead1; ListNode Dummy=NewListNode (0); ListNode Pre=dummy; ListNode ptr1=Head1; ListNode PTR2=head2;  while(Ptr1! =NULL&& PTR2! =NULL) {                        if(Ptr1.val <=ptr2.val) {Pre.next=ptr1; Pre=Pre.next; PTR1=Ptr1.next; } Else{Pre.next=ptr2; Pre=Pre.next; PTR2=Ptr2.next; }        }                if(PTR1 = =NULL) Pre.next=ptr2; ElsePre.next=ptr1; returnDummy.next; }}

[leetcode#148] 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.