题解 | #链表内指定区间反转# [P2]
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
找到第一个要反转的数之前的数 设置为pre.
然后正常反转 (n-m-1)次
https://blog.nowcoder.net/n/82d1e33b25c84859b6442702d266e817
import java.util.*;
public class Solution {
public ListNode reverseBetween (ListNode head, int m, int n) {
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
for(int i=0;i<m-1;i++){
pre = pre.next;
}
ListNode cur = pre.next;
for(int i=0; i<n-m; i++){
ListNode tmp = cur.next;
cur.next = tmp.next;
tmp.next = pre.next;
pre.next = tmp;
}
return dummyNode.next;
}
}