-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (30 loc) · 803 Bytes
/
Solution.java
File metadata and controls
30 lines (30 loc) · 803 Bytes
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
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
List<ListNode> list=f(head, new ArrayList<ListNode>());
list.get(m-1).next=list.get(n-1).next;
for (int i = n-1; i >=m; i--) {
list.get(i).next=list.get(i-1);
}
if (m!=1) {
list.get(m-2).next=list.get(n-1);
return list.get(0);
}else {
return list.get(n-1);
}
}
public List<ListNode> f(ListNode head,List<ListNode> list){
if (head!=null) {
list.add(head);
list=f(head.next,list);
}
return list;
}
}