<jupyter_start><jupyter_text>92. Reverse Linked List II  ContentGiven the head of a singly linked list and two integers left and right where left &lt;= right, reverse the nodes of the list from position left to position right, and return the reversed list.&nbsp;Example 1:Input: head = [1,2,3,4,5], left = 2, right = 4Output: [1,4,3,2,5]Example 2:Input: head = [5], left = 1, right = 1Output: [5]&nbsp;Constraints:	The number of nodes in the list is n.	1 &lt;= n &lt;= 500	-500 &lt;= Node.val &lt;= 500	1 &lt;= left &lt;= right &lt;= n&nbsp;Follow up: Could you do it in one pass?  Difficulty: Medium, AC rate: 42.2% Question Tags:- Linked List Links: 🎁 [Question Detail](https://leetcode.com/problems/reverse-linked-list-ii/description/) | 🎉 [Question Solution](https://leetcode.com/problems/reverse-linked-list-ii/solution/) | 💬 [Question Discussion](https://leetcode.com/problems/reverse-linked-list-ii/discuss/?orderBy=most_votes) Hints:  Sample Test Case[1,2,3,4,5]24 ---What's your idea?在 206 reverse linked list 的基础上，多一些左右两端边界节点的记录---<jupyter_code>from typing import Optional

# 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: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
        if left == right or not head or not head.next:
            return head
        
        cnt = 0
        dummy = ListNode(0, head)
        p = dummy
        while cnt < left:
            cnt += 1
            left_prev = p
            p = p.next
        left_node = p
        
        prev = None
        while cnt < right+1:
            cnt += 1
            p.next, prev, p = prev, p, p.next
            
        left_prev.next = prev
        left_node.next = p
        
        return dummy.next
import sys, os; sys.path.append(os.path.abspath('..'))
from helper import array_to_list, print_list

s = Solution()
print_list(s.reverseBetween(array_to_list([5]), 1, 1))
print_list(s.reverseBetween(array_to_list([1,2]), 1, 2))
print_list(s.reverseBetween(array_to_list([1,2,3]), 1, 2))
print_list(s.reverseBetween(array_to_list([1,2,3,4]), 2, 3))
print_list(s.reverseBetween(array_to_list([1,2,3,4,5]), 2, 4))
import sys, os; sys.path.append(os.path.abspath('..'))
from submitter import submit
submit(92)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13590604026845637, 'fraction_numerical': 0.03229865771812081, 'mean_word_length': 3.2437722419928825, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '358346', 'license': 'MIT', 'n_tokens_mistral': 879, 'n_tokens_neox': 828, 'n_words': 232, 'path': '51-100/92.reverse-linked-list-ii.ipynb', 'repo_name': 'Sorosliu1029/LeetCode', 'size': '2384'}