@@ -45,29 +45,30 @@ class Solution:
4545 """
4646 if head is None or head.next is None :
4747 return
48- slow, fast = head, head.next
48+
4949 # 快慢指针找到链表中点
50+ slow, fast = head, head.next
5051 while fast and fast.next:
5152 slow, fast = slow.next, fast.next.next
53+
54+ # cur 指向右半部分链表
5255 cur = slow.next
5356 slow.next = None
57+
58+ # 反转右半部分链表
5459 pre = None
55- # cur 指向右半部分的链表,反转
5660 while cur:
5761 t = cur.next
5862 cur.next = pre
59- pre = cur
60- cur = t
63+ pre, cur = cur, t
6164 cur = head
65+ # 此时 cur, pre 分别指向链表左右两半的第一个节点
6266
63- # 将左右链表依次连接
6467 while pre:
65- t1 = cur.next
68+ t = pre.next
69+ pre.next = cur.next
6670 cur.next = pre
67- cur = t1
68- t2 = pre.next
69- pre.next = t1
70- pre = t2
71+ cur, pre = pre.next, t
7172```
7273
7374### ** Java**
@@ -96,8 +97,10 @@ class Solution {
9697 slow = slow. next;
9798 fast = fast. next. next;
9899 }
100+
99101 ListNode cur = slow. next;
100102 slow. next = null ;
103+
101104 ListNode pre = null ;
102105 while (cur != null ) {
103106 ListNode t = cur. next;
@@ -106,18 +109,159 @@ class Solution {
106109 cur = t;
107110 }
108111 cur = head;
112+
109113 while (pre != null ) {
110- ListNode t1 = cur. next;
114+ ListNode t = pre. next;
115+ pre. next = cur. next;
111116 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;
116119 }
117120 }
118121}
119122```
120123
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+
121265### ** ...**
122266
123267```
0 commit comments