Reverse a linked list from positionmton. Do it in-place and in one-pass.
ListNode temp = cur.next;
cur.next = prev;
prev = cur;
cur = temp;
头插法?不断把cur后面的Node插入prev的后面
ListNode move = cur.next;
cur.next = move.next;
move.next = prev.next;
prev.next = move;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy, cur;
for(int i = 0; i < m - 1; i ++){
if(prev != null)
prev = prev.next;
}
cur = prev.next;
for(int i = m; i < n; i ++){
ListNode move = cur.next;
cur.next = move.next;
move.next = prev.next;
prev.next = move;
}
return dummy.next;
}
}