题解
迭代版
/*** Definition for singly-linked list.* class ListNode {* val: number* next: ListNode | null* constructor(val?: number, next?: ListNode | null) {* this.val = (val===undefined ? 0 : val)* this.next = (next===undefined ? null : next)* }* }*/function reverseBetween(head: ListNode | null, m: number, n: number): ListNode | null {if (!head) return null;let target = head;let pre = null;while (m > 1) {pre = target;target = target.next;m--;n--;}let a = pre;let b = target;while (n) {let temp = target.next;target.next = pre;pre = target;target = temp;n--;}if (a) {a.next = pre;} else {head = pre;}b.next = target;return head;};
递归版

