首页 > 其他 > 详细

leetcode——92. 反转链表 II

时间:2019-11-01 17:16:51      阅读:97      评论:0      收藏:0      [点我收藏+]
# 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
执行用时 :20 ms, 在所有 python 提交中击败了80.59%的用户
内存消耗 :12 MB, 在所有 python 提交中击败了16.05%的用户
 
——2019.11.01

leetcode——92. 反转链表 II

原文:https://www.cnblogs.com/taoyuxin/p/11777171.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!