92. 反转链表 II(Python)

image

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
	def reverseBetween(self, head, left, right):
		"""
		:type head: ListNode
		:type left: int
		:type right: int
		:rtype: ListNode
		"""    
		right = right -1
		left = left -1

		while( right-left>=0 ):
			print(right-left)
			# 左侧节点
			l = head
			for i in range(left):
				l = l.next
			# 右侧节点
			r = head
			for i in range(right):
				r = r.next
			# print(l.val, r.val)
			l.val, r.val = r.val, l.val
			right = right-1
			left = left+1
		print(head)
		return head
原文地址:https://www.cnblogs.com/DullRabbit/p/14556457.html