/* Author: King, wangjingui@outlook.com Date: Jan 02, 2015 Problem: Insertion Sort List Difficulty: Easy Source: http://oj.leetcode.com/problems/insertion-sort-list/ Notes: Sort a linked list using insertion sort. Solution: ... */ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; ListNode dummy = new ListNode(Integer.MIN_VALUE); dummy.next = head; ListNode cur = head.next; head.next = null; while (cur != null) { ListNode tmp = dummy; while (tmp.next != null && tmp.next.val <= cur.val) tmp = tmp.next; ListNode next = cur.next; cur.next = tmp.next; tmp.next = cur; cur = next; } return dummy.next; } }