mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-03 06:21:33 +00:00
添加Go语言题解
This commit is contained in:
@@ -257,3 +257,36 @@ class Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Go Code:
|
||||
|
||||
```go
|
||||
func insertionSortList(head *ListNode) *ListNode {
|
||||
if head == nil || head.Next == nil { return head }
|
||||
root := &ListNode{
|
||||
Next: head,
|
||||
}
|
||||
cur, nxt := head, head.Next
|
||||
for nxt != nil {
|
||||
// 有序的不需要换位置
|
||||
if cur.Val <= nxt.Val {
|
||||
cur = cur.Next
|
||||
nxt = nxt.Next
|
||||
continue
|
||||
}
|
||||
temp := root
|
||||
for temp.Next.Val <= nxt.Val {
|
||||
temp = temp.Next
|
||||
}
|
||||
// 此时找到合适的位置
|
||||
cur.Next = nxt.Next
|
||||
nxt.Next = temp.Next
|
||||
temp.Next = nxt
|
||||
// 继续向下
|
||||
nxt = cur.Next
|
||||
}
|
||||
return root.Next
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user