-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86-PartitionList.go
More file actions
131 lines (119 loc) · 3.16 KB
/
86-PartitionList.go
File metadata and controls
131 lines (119 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
// 86. Partition List
// Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
// You should preserve the original relative order of the nodes in each of the two partitions.
// Example 1:
// Input: head = [1,4,3,2,5,2], x = 3
// Output: [1,2,2,4,3,5]
// Example 2:
// Input: head = [2,1], x = 2
// Output: [1,2]
// Constraints:
// The number of nodes in the list is in the range [0, 200].
// -100 <= Node.val <= 100
// -200 <= x <= 200
import "fmt"
type ListNode struct {
Val int
Next *ListNode
}
// 打印链表
func printListNode(l *ListNode) {
if nil == l {
return
}
for {
if nil == l.Next {
fmt.Print(l.Val)
break
} else {
fmt.Print(l.Val, " -> ")
}
l = l.Next
}
fmt.Println()
}
// 数组创建链表
func makeListNode(arr []int) *ListNode {
if (len(arr) == 0) {
return nil
}
var l = (len(arr) - 1)
var head = &ListNode{arr[l], nil}
for i := l - 1; i >= 0; i-- {
var n = &ListNode{arr[i], head}
head = n
}
return head
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func partition(head *ListNode, x int) *ListNode {
beforeHead := &ListNode{Val: 0, Next: nil}
before := beforeHead
afterHead := &ListNode{Val: 0, Next: nil}
after := afterHead
for head != nil {
if head.Val < x {
before.Next = head
before = before.Next
} else {
after.Next = head
after = after.Next
}
head = head.Next
}
after.Next = nil
before.Next = afterHead.Next
return beforeHead.Next
}
func partition1(head *ListNode, x int) *ListNode {
// 判断边界
if head == nil || head.Next == nil {
return head
}
// 之前的链表 初始化
beforeHead := &ListNode{Val: 0, Next: nil}
before := beforeHead
// 之后的链表节点 初始化
afterHead := &ListNode{Val: 0, Next: nil}
after := afterHead
for head != nil {
if head.Val < x { // n1
before.Next = head
before = before.Next
} else { // n2
after.Next = head
after = after.Next
}
head = head.Next
}
after.Next = nil
before.Next = afterHead.Next
return beforeHead.Next
}
func main() {
// Example 1:
// Input: head = [1,4,3,2,5,2], x = 3
// Output: [1,2,2,4,3,5]
l1 := makeListNode([]int{1,4,3,2,5,2})
printListNode(l1) // 1 -> 4 -> 3 -> 2 -> 5 -> 2
printListNode(partition(l1, 3)) // 1 -> 2 -> 2 -> 4 -> 3 -> 5
// Example 2:
// Input: head = [2,1], x = 2
// Output: [1,2]
l2 := makeListNode([]int{2,1})
printListNode(l2) // 2 -> 1
printListNode(partition(l2, 2)) // 1 -> 2
l11 := makeListNode([]int{1,4,3,2,5,2})
printListNode(l11) // 1 -> 4 -> 3 -> 2 -> 5 -> 2
printListNode(partition1(l11, 3)) // 1 -> 2 -> 2 -> 4 -> 3 -> 5
l12 := makeListNode([]int{2,1})
printListNode(l12) // 2 -> 1
printListNode(partition1(l12, 2)) // 1 -> 2
}