java 147.插入排序列表(#)。java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 147.插入排序列表(#)。java相关的知识,希望对你有一定的参考价值。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head==null) return head;
        ListNode dummy = new ListNode(0);
        ListNode p1 = dummy;
        ListNode p2 = head;
        while(p2!=null) {
            ListNode next = p2.next;
            while(p1!=null) {
                if(p1.next == null || p1.next.val>=p2.val) {
                    p2.next = p1.next;
                    p1.next = p2;
                    p1 = dummy;
                    break;
                }
                p1 = p1.next;
            }
            p2 = next;
        }
        return dummy.next;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode walker = head, runner = head, prev = head;
        while(runner.next != null && runner.next.next != null) {
            prev = walker;
            walker = walker.next;
            runner = runner.next.next;
        }
        if (runner.next != null) {
            prev = walker;
            walker = walker.next;
        }
        prev.next = null;
        ListNode left = insertionSortList(head);
        ListNode right = insertionSortList(walker);
        return merge(left, right);
    }
    private ListNode merge(ListNode one, ListNode two) {
        if (one == null) return two;
        if (two == null) return one;
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while(one != null && two != null) {
            if (one.val < two.val) {
                cur.next = one;
                one = one.next;
            } else {
                cur.next = two;
                two = two.next;
            }
            cur = cur.next;
        }
        if (one != null) cur.next = one;
        if (two != null) cur.next = two;
        return dummy.next;
    }
}
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    private ListNode insertHelper(ListNode root, ListNode cur) {
        ListNode pre = root;
        while (pre.next != null && pre.val < cur.val && pre.next.val < cur.val) {
            pre = pre.next;
        }
        if (pre == root && pre.val >= cur.val) { // case : insert in front of pre 
            cur.next = pre;
            return cur;
        } 
        cur.next = pre.next;
        pre.next = cur;
        return root;
    }
    public ListNode insertionSortList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode cur = head.next;
        ListNode root = head;
        root.next = null;
        while (cur != null) {
            ListNode temp = cur;
            cur = cur.next;
            root = insertHelper(root, temp);
            
        }
        return root;
    }
}

以上是关于java 147.插入排序列表(#)。java的主要内容,如果未能解决你的问题,请参考以下文章

java 147.插入排序列表(#)。java

java 147.插入排序列表(#)。java

java 147.插入排序列表(#)。java

java 147.插入排序列表(#)。java

我用java刷 leetcode 147. 对链表进行插入排序

Leetcode 147. Insertion Sort List 插入排序 in Java