/* * @lc app=leetcode id=147 lang=java * * [147] Insertion Sort List */ // @lc code=start /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode insertionSortList(ListNode head) { ListNode dummy = new ListNode(0); dummy.next = null; ListNode t; while(head != null) { t = head; head = head.next; ListNode prev = dummy; while(true) { if(prev.next == null || prev.next.val > t.val) { t.next = prev.next; prev.next = t; break; } else { prev = prev.next; } } } return dummy.next; } } // @lc code=end