题解 | #链表内指定区间反转#

链表内指定区间反转

https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c

from dataclasses import FrozenInstanceError
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @param m int整型 
# @param n int整型 
# @return ListNode类
#
class Solution:
    def reverseBetween(self , head: ListNode, m: int, n: int) -> ListNode:
        # write code here
        if not head:
            return None
        if m == n:
            return head
        count = 1
        temp1 = head
        pre = None
        after = head
        while head:
            # 保存断掉的节点
            # 1 连接 4; 4 -》 head_connect.next = first
            # 2 连接 5; 5 -》 end.next = tail_connect
            #
            if count == m:
                head_connect = pre
                end = after
            if count == n:
                tail_connect = after.next
                first = after
            # 可能会导致断链 after == head
            # 3 个元素 调整2次链接
            if count >= m and count <=n:
                temp = after.next
                after.next = pre
                pre = after
                after = temp
                head = after
            else:
                pre = after
                after = after.next
                head = head.next
            count += 1
        # m == 1 1 链接 5 
        # n == 5 2 链接 Null
        if m == 1:
            end.next = tail_connect
            temp1 = first
        # elif (count - 1) == n:
        #     head_connect.next = first
        #     end.next = None
        # elif m == 1 and (count - 1) == n:
        #     pass
        else:
            end.next = tail_connect
            head_connect.next = first
        return temp1

全部评论

相关推荐

06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
06-23 11:43
门头沟学院 Java
allin校招的烤冷...:我靠,今天中午我也是这个hr隔一个星期发消息给我。问的问题还是一模一样的😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务