将一个单链表进行处理后,所得结果为一有序链表
Solution:
将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序。时间复杂度O(n*n)
public ListNode insertionSortList(ListNode head) { ListNode dummy = new ListNode(0); while (head != null) {
ListNode node = dummy;
while (node.next != null && node.next.val < head.val) {
node = node.next;
}
ListNode temp = head.next;
head.next = node.next;
node.next = head;
head = temp;
} return dummy.next;
}