Leetcode's insertion Sort List

Source: Internet
Author: User

Sort a linked list using insertion sort.

The problem is to use a linked list to do the insertion sort. Although the previous data structure learned the insertion and deletion of linked lists, but those are the simplest basic operation, just give a node, directly give the insertion position.

First of all, the idea of inserting a sort is to insert an element into a sorted, ordered list each time, so first find the element that needs to be inserted, and if the linked order of the linked list is ordered in sequence, direct continue

When the element to be inserted is found, the first element that makes it unordered. The position of the current element should be recorded first, as well as the position before the element.

Next, start traversing the sorted list of previous lists until you find the location you want to insert

Finally, we use the basic operation of the insert that I learned before.

The whole idea is relatively clear, just be sure to remember when to insert the next order of time.

One thing you don't understand is why return to Newhead.next?

Attached below the source code

Public ListNode insertionsortlist (ListNode head) {        if (head==null| | Head.next==null) {            return head;        }        ListNode newhead = new ListNode ( -1);        Newhead.next = head;        ListNode cur = head;        ListNode post = Head.next;        while (post!=null) {            if (post.val>cur.val) {                cur = cur.next;                Post = Post.next;            }            else{                ListNode insertcur = newhead;                ListNode insertpost = Newhead.next;                while (insertpost.val<post.val) {                    insertcur = Insertcur.next;                    Insertpost = Insertpost.next;                }                Cur.next = Post.next;                Post.next = Insertpost;                Insertcur.next = post;                Post = Cur.next;                            }        }        return newhead.next;    }

  

Leetcode insertion 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.