题解 | #链表内指定区间反转#
链表内指定区间反转
http://www.nowcoder.com/practice/b58434e200a648c589ca2063f1faf58c
import java.util.*; public class Solution { public ListNode reverseBetween (ListNode head, int m, int n) { // write code here if(head==null||head.next==null||m==n) return head; ListNode newHead = new ListNode(0); ListNode pre = newHead; pre.next = head; for(int i = 1 ; i < m ; i++){ pre = pre.next; } ListNode cur = pre.next; for(int i = m ; i < n ;i++){ ListNode temp = cur.next; cur.next = temp.next; temp.next = pre.next; pre.next = temp; } return newHead.next; } }