Sort a linked list using insertion sort.
- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
- It repeats until no input elements remain.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
Example 2:
Input: -1->5->3->4->0
Output: -1->0->3->4->5
Tags: Linked List
对一个单链表进行插入排序。
构造一个新的链表,每次将原链表中一个节点插入到新链表中合适的位置
func insertionSortList(head *ListNode) *ListNode {
if nil == head {
return nil
}
newHead := new(ListNode)
newHead.Next = &ListNode{head.Val, nil}
head = head.Next
for head != nil {
prev, current := newHead, newHead.Next
for current != nil {
if head.Val < current.Val {
prev.Next = &ListNode{head.Val, current}
break
}
prev, current = prev.Next, current.Next
}
if nil == current && head.Val >= prev.Val {
prev.Next = &ListNode{head.Val, nil}
}
head = head.Next
}
return newHead.Next
}
Sort a linked list using insertion sort.

**Tags:** Math, String
## 题意
> 使用插入排序对一个链表排序。
## 题解
### 思路1
> 。。。。
```go
思路2 ```go
```
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:awesome-golang-algorithm