@@ -45,29 +45,30 @@ class Solution:
45
45
"""
46
46
if head is None or head.next is None :
47
47
return
48
- slow, fast = head, head.next
48
+
49
49
# 快慢指针找到链表中点
50
+ slow, fast = head, head.next
50
51
while fast and fast.next:
51
52
slow, fast = slow.next, fast.next.next
53
+
54
+ # cur 指向右半部分链表
52
55
cur = slow.next
53
56
slow.next = None
57
+
58
+ # 反转右半部分链表
54
59
pre = None
55
- # cur 指向右半部分的链表,反转
56
60
while cur:
57
61
t = cur.next
58
62
cur.next = pre
59
- pre = cur
60
- cur = t
63
+ pre, cur = cur, t
61
64
cur = head
65
+ # 此时 cur, pre 分别指向链表左右两半的第一个节点
62
66
63
- # 将左右链表依次连接
64
67
while pre:
65
- t1 = cur.next
68
+ t = pre.next
69
+ pre.next = cur.next
66
70
cur.next = pre
67
- cur = t1
68
- t2 = pre.next
69
- pre.next = t1
70
- pre = t2
71
+ cur, pre = pre.next, t
71
72
```
72
73
73
74
### ** Java**
@@ -96,8 +97,10 @@ class Solution {
96
97
slow = slow. next;
97
98
fast = fast. next. next;
98
99
}
100
+
99
101
ListNode cur = slow. next;
100
102
slow. next = null ;
103
+
101
104
ListNode pre = null ;
102
105
while (cur != null ) {
103
106
ListNode t = cur. next;
@@ -106,18 +109,159 @@ class Solution {
106
109
cur = t;
107
110
}
108
111
cur = head;
112
+
109
113
while (pre != null ) {
110
- ListNode t1 = cur. next;
114
+ ListNode t = pre. next;
115
+ pre. next = cur. next;
111
116
cur. next = pre;
112
- cur = t1;
113
- ListNode t2 = pre. next;
114
- pre. next = cur;
115
- pre = t2;
117
+ cur = pre. next;
118
+ pre = t;
116
119
}
117
120
}
118
121
}
119
122
```
120
123
124
+ ### ** C#**
125
+
126
+ ``` cs
127
+ /**
128
+ * Definition for singly-linked list.
129
+ * public class ListNode {
130
+ * public int val;
131
+ * public ListNode next;
132
+ * public ListNode(int val=0, ListNode next=null) {
133
+ * this.val = val;
134
+ * this.next = next;
135
+ * }
136
+ * }
137
+ */
138
+ public class Solution {
139
+ public void ReorderList (ListNode head ) {
140
+ if (head == null || head .next == null )
141
+ {
142
+ return ;
143
+ }
144
+ ListNode slow = head ;
145
+ ListNode fast = head .next ;
146
+ while (fast != null && fast .next != null )
147
+ {
148
+ slow = slow .next ;
149
+ fast = fast .next .next ;
150
+ }
151
+
152
+ ListNode cur = slow .next ;
153
+ slow .next = null ;
154
+
155
+ ListNode pre = null ;
156
+ while (cur != null )
157
+ {
158
+ ListNode t = cur .next ;
159
+ cur .next = pre ;
160
+ pre = cur ;
161
+ cur = t ;
162
+ }
163
+ cur = head ;
164
+
165
+ while (pre != null )
166
+ {
167
+ ListNode t = pre .next ;
168
+ pre .next = cur .next ;
169
+ cur .next = pre ;
170
+ cur = pre .next ;
171
+ pre = t ;
172
+ }
173
+ }
174
+ }
175
+ ```
176
+
177
+ ### ** Go**
178
+
179
+
180
+ ``` go
181
+ /* *
182
+ * Definition for singly-linked list.
183
+ * type ListNode struct {
184
+ * Val int
185
+ * Next *ListNode
186
+ * }
187
+ */
188
+ func reorderList (head *ListNode ) {
189
+ if head == nil || head.Next == nil {
190
+ return
191
+ }
192
+ slow , fast := head, head.Next
193
+ for fast != nil && fast.Next != nil {
194
+ slow, fast = slow.Next , fast.Next .Next
195
+ }
196
+
197
+ cur := slow.Next
198
+ slow.Next = nil
199
+
200
+ var pre *ListNode
201
+ for cur != nil {
202
+ t := cur.Next
203
+ cur.Next = pre
204
+ pre, cur = cur, t
205
+ }
206
+ cur = head
207
+
208
+ for pre != nil {
209
+ t := pre.Next
210
+ pre.Next = cur.Next
211
+ cur.Next = pre
212
+ cur, pre = pre.Next , t
213
+ }
214
+ }
215
+ ```
216
+
217
+ ### ** JavaScript**
218
+
219
+ ``` js
220
+ /**
221
+ * Definition for singly-linked list.
222
+ * function ListNode(val, next) {
223
+ * this.val = (val===undefined ? 0 : val)
224
+ * this.next = (next===undefined ? null : next)
225
+ * }
226
+ */
227
+ /**
228
+ * @param {ListNode} head
229
+ * @return {void} Do not return anything, modify head in-place instead.
230
+ */
231
+ var reorderList = function (head ) {
232
+ if (! head || ! head .next ) {
233
+ return ;
234
+ }
235
+ let slow = head;
236
+ let fast = head .next ;
237
+ while (fast && fast .next ) {
238
+ slow = slow .next ;
239
+ fast = fast .next .next ;
240
+ }
241
+
242
+ let cur = slow .next ;
243
+ slow .next = null ;
244
+
245
+ let pre = null ;
246
+ while (cur) {
247
+ const t = cur .next ;
248
+ cur .next = pre;
249
+ pre = cur;
250
+ cur = t;
251
+ }
252
+ cur = head;
253
+
254
+ while (pre) {
255
+ const t = pre .next ;
256
+ pre .next = cur .next ;
257
+ cur .next = pre;
258
+ cur = pre .next ;
259
+ pre = t;
260
+ }
261
+ };
262
+ ```
263
+
264
+
121
265
### ** ...**
122
266
123
267
```
0 commit comments