-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseNode3.java
More file actions
36 lines (27 loc) · 778 Bytes
/
ReverseNode3.java
File metadata and controls
36 lines (27 loc) · 778 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
31
32
33
34
35
36
package DP;
import linkedList.doublePointer.ListNode;
public class ReverseNode3 {
private ListNode successor=null;
public ListNode reverseBetween(ListNode list, int m, int n) {
if(list==null || m<0 || n<0 || m>=n) {
return list;
}
ListNode p=list;
for(int i=0;i<m-1;i++) {
p = p.next;
}
ListNode last = reverse(p.next, n-m);
p.next = last;
return list;
}
public ListNode reverse(ListNode list, int n) {
if(list==null || list.next==null || n<=1) {
successor = list.next;
return list;
}
ListNode last = reverse(list.next, n-1);
list.next.next = list;
list.next = successor;
return last;
}
}