Skip to content

Commit 121ba97

Browse files
committed
add leetcode 147
1 parent 159547e commit 121ba97

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
type ListNode struct {
9+
Val int
10+
Next *ListNode
11+
}
12+
13+
// 比较结果
14+
func isEqual(l1 *ListNode, l2 *ListNode) bool {
15+
16+
for l1 != nil && l2 != nil {
17+
if l1.Val != l2.Val {
18+
return false
19+
}
20+
l1 = l1.Next
21+
l2 = l2.Next
22+
}
23+
if l1 == nil && l2 != nil {
24+
return false
25+
}
26+
if l1 != nil && l2 == nil {
27+
return false
28+
}
29+
return true
30+
}
31+
32+
func PrintList(head *ListNode) {
33+
for head != nil {
34+
fmt.Print(head.Val, "->")
35+
head = head.Next
36+
}
37+
fmt.Println()
38+
}
39+
40+
// 根据数组反序列化链表
41+
func UnmarshalListBySlice(nums []int) *ListNode {
42+
head := &ListNode{Val: -1, Next: nil}
43+
tmp := head
44+
for _, v := range nums {
45+
tmp.Next = &ListNode{Val: v, Next: nil}
46+
tmp = tmp.Next
47+
}
48+
return head.Next
49+
}
50+
51+
// 随机初始化链表
52+
func UnmarshalListByRand(max_num int, len int) *ListNode {
53+
head := &ListNode{Val: -1, Next: nil}
54+
tmp := head
55+
56+
for i := 0; i < len; i++ {
57+
tmp.Next = &ListNode{Val: rand.Intn(max_num), Next: nil}
58+
tmp = tmp.Next
59+
}
60+
return head.Next
61+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [147. Insertion Sort List][title]
2+
3+
## Description
4+
Sort a linked list using insertion sort.
5+
6+
### Algorithm of Insertion Sort:
7+
1. Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
8+
1. 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.
9+
1. It repeats until no input elements remain.
10+
11+
**Example 1:**
12+
```
13+
Input: 4->2->1->3
14+
Output: 1->2->3->4
15+
```
16+
17+
**Example 2:**
18+
```
19+
Input: -1->5->3->4->0
20+
Output: -1->0->3->4->5
21+
```
22+
23+
**Tags:** Linked List
24+
25+
## 题意
26+
> 对一个单链表进行插入排序。
27+
28+
## 题解
29+
30+
### 思路 1
31+
> 构造一个新的链表,每次将原链表中一个节点插入到新链表中合适的位置
32+
33+
```go
34+
func insertionSortList(head *ListNode) *ListNode {
35+
if nil == head {
36+
return nil
37+
}
38+
newHead := new(ListNode)
39+
newHead.Next = &ListNode{head.Val, nil}
40+
41+
head = head.Next
42+
for head != nil {
43+
prev, current := newHead, newHead.Next
44+
for current != nil {
45+
if head.Val < current.Val {
46+
prev.Next = &ListNode{head.Val, current}
47+
break
48+
}
49+
prev, current = prev.Next, current.Next
50+
}
51+
if nil == current && head.Val >= prev.Val {
52+
prev.Next = &ListNode{head.Val, nil}
53+
}
54+
head = head.Next
55+
}
56+
return newHead.Next
57+
}
58+
```
59+
60+
## 结语
61+
62+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
63+
64+
[title]: https://leetcode.com/problems/insertion-sort-list/
65+
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Solution
2+
3+
func Solution(head *ListNode) *ListNode {
4+
if nil == head {
5+
return nil
6+
}
7+
newHead := new(ListNode)
8+
newHead.Next = &ListNode{head.Val, nil}
9+
10+
head = head.Next
11+
for head != nil {
12+
prev, current := newHead, newHead.Next
13+
for current != nil {
14+
if head.Val < current.Val {
15+
prev.Next = &ListNode{head.Val, current}
16+
break
17+
}
18+
prev, current = prev.Next, current.Next
19+
}
20+
if nil == current && head.Val >= prev.Val {
21+
prev.Next = &ListNode{head.Val, nil}
22+
}
23+
head = head.Next
24+
}
25+
return newHead.Next
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package Solution
2+
3+
import "testing"
4+
5+
func TestSolution(t *testing.T) {
6+
TestCases := []struct{
7+
name string
8+
inputs []int
9+
expect []int
10+
}{
11+
{
12+
"TestCase 1",
13+
[]int{4,2,1,3},
14+
[]int{1,2,3,4},
15+
},
16+
{
17+
"TestCase 2",
18+
[]int{-1,5,3,4,0},
19+
[]int{-1,0,3,4,5},
20+
},
21+
}
22+
23+
for _, c := range TestCases {
24+
t.Run(c.name, func(t *testing.T) {
25+
ret := Solution(UnmarshalListBySlice(c.inputs))
26+
if !isEqual(ret, UnmarshalListBySlice(c.expect)) {
27+
PrintList(ret)
28+
PrintList(UnmarshalListBySlice(c.expect))
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, ret, c.inputs)
30+
}
31+
})
32+
}
33+
}

0 commit comments

Comments
 (0)