-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution92.java
More file actions
53 lines (53 loc) · 1.54 KB
/
Solution92.java
File metadata and controls
53 lines (53 loc) · 1.54 KB
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
48
49
50
51
52
53
/**
* Created by Alex on 2017/3/19.
*/
public class Solution92 {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(m == n){
return head;
}
int position = 1;
ListNode preNode = null;
ListNode currentNode = head;
ListNode innerTail = null;
ListNode innerHead = null;
ListNode tempNode;
ListNode lNode = null;
ListNode rNode = null;
while(currentNode != null && position <= n){
if(position >= m){
if(position == m){
innerTail = currentNode;
preNode = currentNode;
currentNode = currentNode.next;
}else if(position == n){
rNode = currentNode.next;
innerHead = currentNode;
innerHead.next = preNode;
break;
}else {
tempNode = currentNode;
currentNode = currentNode.next;
tempNode.next = preNode;
preNode = tempNode;
}
}else {
preNode = currentNode;
lNode = currentNode;
currentNode = currentNode.next;
}
position++;
}
if(lNode != null){
lNode.next = innerHead;
}
if(innerTail != null){
innerTail.next = rNode;
}
if(m == 1){
return innerHead;
}else {
return head;
}
}
}