Skip to content

Latest commit

 

History

History
42 lines (34 loc) · 1000 Bytes

File metadata and controls

42 lines (34 loc) · 1000 Bytes

92. 反转链表 II

反转从位置 mn 的链表。请使用一趟扫描完成反转。

说明:

1 ≤ mn ≤ 链表长度。

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL

题解 (Python)

1. 题解

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
        dummy = ListNode(next=head)
        start = curr = dummy
        next = curr.next

        for _ in range(m - 1):
            start = curr = next
            next = curr.next

        for _ in range(n - m + 1):
            prev = curr
            curr = next
            next = curr.next
            curr.next = prev

        start.next.next = next
        start.next = curr

        return dummy.next