题解 | #链表内指定区间反转#
链表内指定区间反转
https://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
public ListNode reverseBetween (ListNode head, int m, int n) { if(head==null || m == n){ return head; } ListNode cur = head; ListNode last = null; int i = 1; while(i < m){ last = cur; cur = cur.next; i++; } ListNode pre = null; ListNode end = cur; while(i <= n){ ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; i++; } end.next = cur; if(last!=null){ last.next = pre; }else{ head = pre; } return head; }