Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
ListNode result = new ListNode(0);
while(head != null){
ListNode pre = result;
ListNode cur = result.next;
while(cur != null && head.val > cur.val){
pre = cur;
cur = cur.next;
}
ListNode newNode = new ListNode(head.val);
newNode.next = cur;
pre.next = newNode;
head = head.next;
}
return result.next;
}
}