1 class Solution:
 2     def constructLink(self,lists):
 3         n = len(lists)
 4         if n == 0:
 5             return None
 6         if n == 1:
 7             return ListNode(lists[0])
 8         
 9         head = ListNode(lists[-1])
10         for i in range(n-2,-1,-1):
11             cur = ListNode(lists[i])
12             cur.next = head 
13             head = cur
14         return head
15     
16     def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
17         lists = []
18         if m == n:
19             return head
20         while head != None:
21             lists.append(head.val)
22             head = head.next
23         reorder = []
24         if m == 1:
25             reorder = lists[n-1::-1] + lists[n:]
26         else:
27             reorder = lists[:m-1] + lists[n-1:m-2:-1] + lists[n:]
28         return self.constructLink(reorder)

 

posted on 2019-12-10 20:07  Sempron2800+  阅读(156)  评论(0编辑  收藏  举报