📌  相关文章
📜  教资会网络 | NTA UGC NET 2019 年 6 月 – II |问题 10(1)

📅  最后修改于: 2023-12-03 15:39:53.923000             🧑  作者: Mango

NTA UGC NET 2019 年 6 月 - II | 问题 10

简介

NTA UGC NET 2019 年 6 月 - II 是一项印度国家资格考试,考察的是大学和学院的担任教授、副教授、讲师等教学职位的能力。问题 10 主要考察了计算机科学和应用中的程序设计概念。

问题描述

下面是问题 10 的描述:

假设我们有一个链表:head->1->2->3->4,如果我们按照如下方式反转其中的一部分:2->3->4,那么反转之后的链表应该为:head->1->4->3->2。

请构造一个函数 reverseSubList(head, start, end),该函数用来反转从 start 到 end 之间的链表结点。例如:

head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
reverseSubList(head, 2, 4)

应该返回反转后的链表:1->4->3->2。

请在以下的选项中选择正确的实现:

A.

def reverseSubList(head, start, end):
    """
    :type head: ListNode
    :type start: int
    :type end: int
    :rtype: ListNode
    """
    if not head or not head.next or start >= end:
        return head

    pre_head = ListNode(-1)
    pre_head.next = head
    pre = pre_head

    for i in range(start - 1):
        pre = pre.next

    cur = pre.next
    next_node = cur.next

    for i in range(end - start):
        cur.next = next_node.next
        next_node.next = pre.next
        pre.next = next_node
        next_node = cur.next

    return pre_head.next

B.

def reverseSubList(head, start, end):
    """
    :type head: ListNode
    :type start: int
    :type end: int
    :rtype: ListNode
    """
    if not head or not head.next or start >= end:
        return head

    pre_head = ListNode(-1)
    pre_head.next = head
    pre = pre_head

    for i in range(start - 1):
        pre = pre.next

    cur = pre.next
    for i in range(end - start):
        next_node = cur.next
        cur.next = next_node.next
        pre.next = next_node
        next_node.next = cur

    return pre_head.next

C.

def reverseSubList(head, start, end):
    """
    :type head: ListNode
    :type start: int
    :type end: int
    :rtype: ListNode
    """
    if not head or not head.next:
        return head
    if start == end:
        return head

    cur = head
    for i in range(1, start):
        if not cur:
            return head
        if i == start - 1:
            pre_start = cur
        cur = cur.next

    start_node = cur
    end_node = start_node
    for i in range(start, end):
        if not end_node:
            return head
        end_node = end_node.next

    if not end_node:
        pre_start.next = None
        reverseLinkedList(start_node)
        pre_start.next = start_node
        return head

    next_node = end_node.next
    end_node.next = None
    reverseLinkedList(start_node)
    pre_start.next = end_node
    start_node.next = next_node

    return head


def reverseLinkedList(head):
    pre_node = None
    cur_node = head
    while cur_node:
        tmp_node = cur_node.next
        cur_node.next = pre_node
        pre_node = cur_node
        cur_node = tmp_node

D.

def reverseSubList(head, start, end):
    """
    :type head: ListNode
    :type start: int
    :type end: int
    :rtype: ListNode
    """
    if not head or not head.next:
        return head

    pre_head = ListNode(None)
    pre_head.next = head
    pre = pre_head

    for i in range(1, start):
        if not pre:
            return head
        pre = pre.next

    start_node = pre.next
    if not start_node:
        return head

    for i in range(start, end):
        if not start_node.next:
            return head
        next_node = start_node.next
        start_node.next = next_node.next
        next_node.next = pre.next
        pre.next = next_node

    return pre_head.next
参考资料

该问题来源于 NTA UGC NET 2019 年 6 月 - II 计算机科学和应用中的第 10 个问题

以上代码实现参考自 leetcode 92. Reverse Linked List II 解题报告