"Leetcode-Interview algorithm classic-java implementation" "147-insertion sort list (List insert sort)"

Source: Internet
Author: User

"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)"

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.