leetcode--Insertion Sort List

Sort a linked list using insertion sort.
/**
 * 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 null;
		ListNode p = new ListNode(Integer.MIN_VALUE);
		ListNode cur = head;
		ListNode t = p;
		while(cur!=null){
			ListNode n = cur.next;
			t = p;
			while(t.next!=null&&t.next.val<cur.val){
				t = t.next;
			}
			cur.next = t.next;
			t.next = cur;
			cur = n;
		}
		return p.next;
    }
}

你可能感兴趣的:(leetcode--Insertion Sort List)