/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode InsertionSortList(ListNode head) {
if(head == null || head.next == null){
return head;
}
var list = new SortedNodes();
while(head != null){
list.Add(head.val);
head = head.next;
}
ListNode h = null;
ListNode node = null;
var c = 0;
foreach(var n in list.Nodes){
if(c == 0){
node = new ListNode(n);
h = node;
}else{
node.next = new ListNode(n);
node = node.next;
}
c++;
}
return h;
}
public class SortedNodes{
private IList<int> _nodes;
public SortedNodes(){
_nodes = new List<int>();
}
public void Add(int n)
{
for(var i = 0;i < _nodes.Count; i++){
if(n < _nodes[i]){
_nodes.Insert(i,n);
return;
}
}
_nodes.Add(n);
}
public IList<int> Nodes{
get{
return _nodes;
}
}
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
LeetCode -- Insertion Sort List
原文:http://blog.csdn.net/lan_liang/article/details/49108365