[leetcode] Insertion Sort List

Sort a linked list using insertion sort.

https://oj.leetcode.com/problems/insertion-sort-list/

 

思路:模拟插入排序,因为没有pre的指针,所以找位置是从前向后找的。

 

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode newHead = new ListNode(-1);
        newHead.next = head;

        ListNode cur = head.next;
        ListNode pre = head;
        while (cur != null) {
            ListNode next = cur.next;
            ListNode p = newHead;
            while (p.next != null && p.next.val < cur.val)
                p = p.next;

            pre.next = next;
            cur.next = p.next;
            p.next = cur;
            if (p == pre)
                pre = pre.next;

            cur = next;
        }

        return newHead.next;
    }

    public static void main(String[] args) {
        ListNode head = ListUtils.makeList(new int[] { 1, 3, 5, 6, 4 });
        ListUtils.printList(head);
        head = new Solution().insertionSortList(head);
        ListUtils.printList(head);

    }

}

 

第二遍记录:之前的做法是直接在原有链表上修改,有点复杂,代码可读性太差。。

修改下实现:新建一个开始dummyHead点,然后依次将原有链表上的元素加进去,每次添加的时候要从新的表头开始扫描到插入的位置。

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head == null)
            return null;
        ListNode helper = new ListNode(0);
        ListNode pre = helper;
        ListNode cur = head;
        while(cur!=null)
        {
            ListNode next = cur.next;
            pre = helper;
            while(pre.next!=null && pre.next.val<cur.val)
            {
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next = cur;
            cur = next;
        }
        return helper.next;
    }
    public static void main(String[] args) {
        ListNode head = ListUtils.makeList(new int[] { 1, 2, 4, 3, 5 });
        ListUtils.printList(head);
        head = new Solution().insertionSortList(head);
        ListUtils.printList(head);

    }

}

 

第三遍:

思路忘记了, 第二遍的思路,新建一个newHead节点,注意这次不跟head连接! newHead后面挂载已经排好序的, 在原链表上依次取节点在newHead中寻找she和插入的位置的插进去。

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null)
            return head;

        ListNode newHead = new ListNode(-1);
        ListNode pre = newHead;

        ListNode cur = head;

        while (cur != null) {
            ListNode post = cur.next;
            pre = newHead;
            while (pre.next != null && pre.next.val < cur.val) {
                pre = pre.next;
            }

            cur.next = pre.next;
            pre.next = cur;

            cur = post;
        }

        return newHead.next;

    }

    public static void main(String[] args) {
        ListNode head = ListUtils.makeList(new int[] { 1, 3, 2, 4 });
        ListUtils.printList(head);
        head = new Solution().insertionSortList(head);
        ListUtils.printList(head);

    }

}

 

 

参考:

http://blog.csdn.net/linhuanmars/article/details/21144553

 

posted @ 2014-07-07 21:20  jdflyfly  阅读(156)  评论(0)    收藏  举报