Sort a linked list in O(n log n) time using constant space complexity.
1. Analysis
This topic mainly examines the link on the merger sorting algorithm.
2, the correct code implementation
PackageCom.edu.leetcode;ImportCom.edu.leetcode.ListNode; Public classSortlist {/** * @paramargs*/ PublicListNode Merge (ListNode first,listnode second) {//merges two ordered linked lists and returns the drop nodes of the new linked listListNode Rear; ListNode Head; Rear=head=NewListNode (-1); while(first!=NULL&&second!=NULL){ if(first.val<=second.val) {Rear.next=First ; Rear=First ; First=First.next; } Else{Rear.next=second; Rear=second; Second=Second.next; } } if(first!=NULL) Rear.next=First ; ElseRear.next=second; returnHead.next; } PublicListNode sortlist (ListNode head) {/** To achieve the consolidation of the list of lists: 1, the linked list into the basic equivalent of two linked list * 2, recursion will continue to divide the two links, until the length of the list is 0 or 1 * 3, call merge () to merge links
*/ if(head==NULL|| head.next==NULL) returnHead; ListNode Mid=Head; ListNode POS=Mid.next; while(pos!=NULL) {pos=Pos.next; if(pos!=NULL) {pos=Pos.next; Mid=Mid.next; }} ListNode Q=sortlist (Mid.next); Mid.next=NULL; returnMerge (Sortlist (head), q); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubListNode First1 =NewListNode (0); ListNode rear1=First1; for(inti=9;i>=1;i--) {ListNode Q=NewListNode (i); Rear1.next=Q; Rear1=Q; } ListNode Q=First1; while(q!=NULL) {System.out.print (Q.val+ ","); Q=Q.next; } System.out.println (); Sortlist SL=Newsortlist (); Sl.sortlist (FIRST1); ListNode P=First1; while(p!=NULL) {System.out.print (P.val+ ","); P=P.next; } System.out.println (); }}
3, some problems of the code
The main problem here is to divide the Sortlist () method above into two steps: Divide () and MergeSort (). Personally feel that it should be the same. But the result of the operation is really different, if the great god to browse, please point to the younger brother.
classlistnode{intVal; ListNode Next=NULL; PublicListNode () {} PublicListNode (intval) { This. val=Val; }} Public classSortlist { PublicListNode Merge (ListNode first,listnode second) {ListNode rear; ListNode Head; Rear=head=NewListNode (); while(first!=NULL&&second!=NULL){ if(first.val<=second.val) {Rear.next=First ; Rear=First ; First=First.next; } Else{Rear.next=second; Rear=second; Second=Second.next; } } if(first!=NULL) Rear.next=First ; ElseRear.next=second; returnHead.next; } PublicListNode Divide (ListNode first) {//divides a linked list into two basic equal sub- listsif(first==NULL) return NULL; ListNode Mid=First ; ListNode POS=Mid.next; while(pos!=NULL) {pos=Pos.next; if(pos!=NULL) {pos=Pos.next; Mid=Mid.next; }} ListNode Q=Mid.next; Mid.next=NULL; returnQ; } Public voidMergeSort (ListNode first) {//merge sortif(first!=NULL&&first.next!=NULL) {ListNode second=Divide (first); Divide the list into two parts mergesort (first); Recursive mergesort (second); Merge (first, second); } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubListNode First1 =NewListNode (0); ListNode rear1=First1; for(inti=9;i>=1;i--) {ListNode Q=NewListNode (i); Rear1.next=Q; Rear1=Q; } ListNode Q=First1; while(q!=NULL) {System.out.print (Q.val+ ","); Q=Q.next; } System.out.println (); Sortlist SL=Newsortlist (); Sl. MergeSort (FIRST1); ListNode P=First1; while(p!=NULL) {System.out.print (P.val+ ","); P=P.next; } System.out.println (); }}
"Leetcode" Sort List Java implementation