class Solution:
def reverseBetween(self, head, m, n):
"""
:type head: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
if head == None or head.next == None:
return head
h = ListNode(-1)
h.next = head
pre = h
cur = head
i = 1
while i < m:
pre = cur
cur = cur.next
i += 1
t1 = pre
t2 = cur
while i <= n:
lat = cur.next
cur.next = pre
pre = cur
cur = lat
i += 1
t1.next = pre
t2.next = cur
return h.next
92. 反转链表 II
最新推荐文章于 2022-07-29 17:38:52 发布