Sloution 1:
split the linked list,then joint it, pay attention to the boundary
# 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, left: int, right: int) -> ListNode:
def reverse(head):
pre = None
cur = head
while cur:
next = cur.next
cur.next = pre
pre = cur
cur = next
return cur
dummy_node = ListNode(-1,head)
pre = dummy_node
for i in range(left-1): #pre is the node before leftnode
pre = pre.next
right_node = pre
for i in range(right-left+1): #right_node is truly rightnode
right_node = right_node.next
left_node = pre.next
curr = right_node.next
pre.next = None
right_node.next = None
reverse(left_node)
pre.next = right_node
left_node.next = curr
return dummy_node.next