题目:
题解:
class Solution:def reorderList(self, head: ListNode) -> None:if not head:returnmid = self.middleNode(head)l1 = headl2 = mid.nextmid.next = Nonel2 = self.reverseList(l2)self.mergeList(l1, l2)def middleNode(self, head: ListNode) -> ListNode:slow = fast = headwhile fast.next and fast.next.next:slow = slow.nextfast = fast.next.nextreturn slowdef reverseList(self, head: ListNode) -> ListNode:prev = Nonecurr = headwhile curr:nextTemp = curr.nextcurr.next = prevprev = currcurr = nextTempreturn prevdef mergeList(self, l1: ListNode, l2: ListNode):while l1 and l2:l1_tmp = l1.nextl2_tmp = l2.nextl1.next = l2l1 = l1_tmpl2.next = l1l2 = l2_tmp