Insertion Sort List
题目:
Sort a linked list using insertion sort.
分析:
无
解法:
public class Solution {
/**
* @param head: The first node of linked list.
* @return: The head of linked list.
*/
public ListNode insertionSortList(ListNode head) {
// write your code here
ListNode dummy = new ListNode(0);
while (head != null) {
ListNode curr = dummy;
while (curr.next != null && curr.next.val < head.val) {
curr = curr.next;
}
ListNode temp = head;
head = head.next;
temp.next = curr.next;
curr.next = temp;
}
return dummy.next;
}
}