题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
from pickle import NONE
from re import template
# 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
def reverseList(pre, cur, nxt, k):
for i in range(k):
cur.next = nxt.next
nxt.next = pre.next
pre.next = nxt
# 步骤顺序不可调换
nxt = cur.next
temp = ListNode(0)
temp.next = head
pre = temp
cur = head
nxt = head.next
for i in range(m-1):
pre = cur
cur = nxt
nxt = nxt.next
reverseList(pre, cur, nxt, n-m)
return temp.next
上海得物信息集团有限公司公司福利 1229人发布