File tree Expand file tree Collapse file tree 3 files changed +129
-0
lines changed Expand file tree Collapse file tree 3 files changed +129
-0
lines changed Original file line number Diff line number Diff line change 150
150
- [ 7.整数反转] ( leetcode/medium/7.go )
151
151
- [ 36.有效的数独] ( leetcode/medium/36.go )
152
152
- [ 73.矩阵置零] ( leetcode/medium/73.go )
153
+ - [ 86.分隔链表] ( leetcode/medium/t86/solution.go )
153
154
- [ 93.复原 IP 地址] ( leetcode/medium/93.go )
154
155
- [ 99.恢复二叉搜索树] ( leetcode/medium/99.go )
155
156
- [ 105.从前序与中序遍历序列构造二叉树] ( leetcode/medium/105.go )
Original file line number Diff line number Diff line change
1
+ package t86
2
+
3
+ /**
4
+ * Definition for singly-linked list.
5
+ * type ListNode struct {
6
+ * Val int
7
+ * Next *ListNode
8
+ * }
9
+ */
10
+
11
+ // 86. 分隔链表
12
+ // https://leetcode.cn/problems/partition-list
13
+ func partition (head * ListNode , x int ) * ListNode {
14
+ // 创建两个链表,分别存储小于x和等于x的节点
15
+ less := & ListNode {}
16
+ more := & ListNode {}
17
+ // 创建两个链表的虚拟头节点 方便插入节点
18
+ lessHead := less
19
+ moreHead := more
20
+ // 遍历链表,将节点插入对应的链表
21
+ for head != nil {
22
+ if head .Val < x {
23
+ // 小于 x 的值放入 less 链表
24
+ less .Next = head
25
+ less = less .Next
26
+ head = head .Next
27
+ // 断开 head 和 less 的连接
28
+ less .Next = nil
29
+ } else {
30
+ // 大于等于 x 的值放入 more 链表
31
+ more .Next = head
32
+ more = more .Next
33
+ head = head .Next
34
+ // 断开 head 和 more 的连接
35
+ more .Next = nil
36
+ }
37
+ }
38
+ // 使用初始化的虚拟头节点 将 more 链表连接在 less 链表之后
39
+ less .Next = moreHead .Next
40
+ // 返回 less 链表虚拟头节点的下一个节点
41
+ return lessHead .Next
42
+ }
43
+
44
+ type ListNode struct {
45
+ Val int
46
+ Next * ListNode
47
+ }
Original file line number Diff line number Diff line change
1
+ package t86
2
+
3
+ import (
4
+ "reflect"
5
+ "testing"
6
+ )
7
+
8
+ func TestPartition (t * testing.T ) {
9
+ tests := []struct {
10
+ name string
11
+ head * ListNode
12
+ x int
13
+ want * ListNode
14
+ }{
15
+ {
16
+ name : "Case 1" ,
17
+ head : & ListNode {
18
+ Val : 1 ,
19
+ Next : & ListNode {
20
+ Val : 4 ,
21
+ Next : & ListNode {
22
+ Val : 3 ,
23
+ Next : & ListNode {
24
+ Val : 2 ,
25
+ Next : & ListNode {
26
+ Val : 5 ,
27
+ Next : & ListNode {
28
+ Val : 2 ,
29
+ },
30
+ },
31
+ },
32
+ },
33
+ },
34
+ },
35
+ x : 3 ,
36
+ want : & ListNode {
37
+ Val : 1 ,
38
+ Next : & ListNode {
39
+ Val : 2 ,
40
+ Next : & ListNode {
41
+ Val : 2 ,
42
+ Next : & ListNode {
43
+ Val : 4 ,
44
+ Next : & ListNode {
45
+ Val : 3 ,
46
+ Next : & ListNode {
47
+ Val : 5 ,
48
+ },
49
+ },
50
+ },
51
+ },
52
+ },
53
+ },
54
+ },
55
+ {
56
+ name : "Case 2" ,
57
+ head : & ListNode {
58
+ Val : 2 ,
59
+ Next : & ListNode {
60
+ Val : 1 ,
61
+ },
62
+ },
63
+ x : 2 ,
64
+ want : & ListNode {
65
+ Val : 1 ,
66
+ Next : & ListNode {
67
+ Val : 2 ,
68
+ },
69
+ },
70
+ },
71
+ }
72
+
73
+ for _ , tt := range tests {
74
+ t .Run (tt .name , func (t * testing.T ) {
75
+ if got := partition (tt .head , tt .x ); ! reflect .DeepEqual (got , tt .want ) {
76
+ //if got := partition(tt.head, tt.x); !equals(got, tt.want) {
77
+ t .Errorf ("partition() = %v, want %v" , got , tt .want )
78
+ }
79
+ })
80
+ }
81
+ }
You can’t perform that action at this time.
0 commit comments