给定一个单链表 L的头节点 head ,单链表 L 表示为:
L0 → L1 → ... → Ln-1 → Ln
请将其重新排列后变为:
L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → ...
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:

输入: head = [1,2,3,4]
输出: [1,4,2,3]
示例 2:

输入: head = [1,2,3,4,5]
输出: [1,5,2,4,3]
提示:
-
链表的长度范围为
[1, 5 * 104] -
1 <= node.val <= 1000Definition for singly-linked list.
class ListNode:
def init(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def reverse(self,head1):
if head1 is None:
return None
pre=None
cur=head1
while cur is not None:
cur_new=cur.next
cur.next=pre
pre=cur
cur=cur_new
return predef reorderList(self, head: ListNode) -> None: """ Do not return anything, modify head in-place instead. """ if head is None: return slow=head fast=head while fast.next is not None and fast.next.next is not None: slow=slow.next fast=fast.next.next half_2=slow.next slow.next=None tail=self.reverse(half_2) cur_h=head while tail is not None: cur_h_new=cur_h.next cur_h.next=tail tail=tail.next cur_h.next.next=cur_h_new cur_h=cur_h_new