本文共 1333 字,大约阅读时间需要 4 分钟。
# 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 , m , n ): # write code here Head = ListNode(0) Head.next = head p = head q = head pre = Head for _ in range(m-1): pre = p p = p.next for _ in range(n-1): q = q.next last = q.next pre.next = last q.next = None rev = self.ReverseList(p) # 反转链表 p_rev = rev while p_rev.next: p_rev = p_rev.next p_rev.next = last pre.next = rev return Head.next def ReverseList(self, pHead): # write code here pre = None while pHead: next = pHead.next pHead.next = pre pre = pHead pHead = next return pre
# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # 返回ListNode def ReverseList(self, pHead): # write code here pre = None while pHead: next = pHead.next pHead.next = pre pre = pHead pHead = next return pre
转载地址:http://idsl.baihongyu.com/