public ListNode reverseBetween(ListNode head, int m, int n) { ListNode dummy = new ListNode(0); //虚拟头结点往往是有必要的,好处非常明显 dummy.next = head; ListNode pre = dummy; for(int i = 1; i < m; i++){ pre = pre.next; } head = pre.next; for(int i = m; i < n; i++){ ListNode nex = head.next; head.next = nex.next; nex.next = pre.next; pre.next = nex; } return dummy.next; }
92. 反转链表 II.反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。
原文:https://www.cnblogs.com/czsblog/p/10804642.html