-
Notifications
You must be signed in to change notification settings - Fork 0
/
Leetcode0092.java
47 lines (41 loc) · 976 Bytes
/
Leetcode0092.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//package com.y2019.m08.d24;
import java.util.ArrayList;
import java.util.List;
/**
* Created by zhangweilong on 2019/08/24
*
* @author zhangweilong@corp.netease.com
* @since 2019/08/24
*/
public class Leetcode0092 {
// 0-1-2-3-4 m=2.n=4
//m-- = 1 n-- =3
//0-3-2-1-4
public ListNode reverseBetween(ListNode head, int m, int n) {
List<ListNode> a = new ArrayList<>();
while (true) {
a.add(head);
head = head.next;
if (head == null) break;
}
m--;
n--;
for (int i = m, j = n; i < j; i++, j--) {
ListNode tmp = a.get(i);
a.set(i, a.get(j));
a.set(j, tmp);
}
for (int i = 1; i < a.size(); i++) {
a.get(i - 1).next = a.get(i);
}
a.get(a.size() - 1).next = null;
return a.get(0);
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}