"147-insertion sort list (linked list insertion sort)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Sort a linked list using insertion sort.
Main Topic
Insert sort on a single-linked list table
Thinking of solving problems
Use a pointer p to point to the first node of the unordered list, find the first node in the sorted part that is greater than or equal to Q, insert p corresponding node in the correct position, and P re-point to the first node of the list that is not ordered. Until the list is finished sorting well.
Code Implementation
Node class
Algorithm implementation class
Public class solution {PublicListNodeInsertionsortlist (ListNodeHead) {ListNoderoot = newListNode(0);//Head node root.Next= head;ListNodep = head;ListNodeQListNodeR while(P! = null && p.Next! = null) {if(P.val <= p.Next. val) {p = p.Next; }Else{q = p.Next; P.Next= Q.Next; R = root;//Find the first node that is greater than or equal to Q.val, as root.NextThere must be such a junction between P. while(R.Next. val <= q.val) {r = R.Next; } q.Next= R.Next; R.Next= q; } }returnRoot.Next; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47801745"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "147-insertion sort list (List insert sort)"