92. 反转链表Ⅱ
题目:


题解:
java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode cur = head;
ListNode part = new ListNode();
int count = 1;
Map<Integer, ListNode> map = new HashMap<>();
while(cur != null) {
map.put(count++, new ListNode(cur.val));
cur = cur.next;
}
cur = new ListNode();
ListNode h = cur;
for(int i=1;i<left;i++) {
cur.next = map.get(i);
cur = cur.next;
}
for(int i=right;i>=left;i--) {
cur.next = map.get(i);
cur = cur.next;
}
for(int i=right+1;i<count;i++) {
cur.next = map.get(i);
cur = cur.next;
}
return h.next;
}
}