# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def reverseBetween(self, head, m, n): """ :type head: ListNode :type m: int :type n: int :rtype: ListNode """ a=ListNode(0) a.next=head q=a p=head s=1 stack=[] while s<m: p=p.next q=q.next s+=1 while s>=m: stack.append(p) p=p.next s+=1 if s==n+1:break while stack: q.next=stack.pop() q=q.next
原文:https://www.cnblogs.com/taoyuxin/p/11777171.html