链表内指定区间反转

在这里插入图片描述
指定区间还弄错…是序号,不是val.

class Solution:
    def reverseBetween(self , head , m , n ):
        # write code here
        # 其实只是对应序号,其实第二个节点没有必要提前找出来~~
        # 如果不细心,再多时间也无法解决~~所以有时候不浪费时间
        # 这么简单的题做了那么久~~足见细心程度
        # 链表操作先给他个头节点,避免出错~
        def reverse(head1, res):
            pre = head1
            head1 = head1.next
            num = 0
            while num < res:
                last = head1.next
                head1.next = pre
                pre = head1
                head1 = last
                num += 1
            return pre, head1
        first = ListNode(5)
        first.next = head
        head1 = head
        pre_head1 = first
        for i in range(2, m+1):
            pre_head1 = pre_head1.next
            head1 = head1.next
        pre, res = reverse(head1, n-m)
        pre_head1.next = pre
        head1.next = res
        return first.next

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值