package com.my.util;
public class SingleNode {
public int value;
public SingleNode next;
public SingleNode(int data){
this.value = data;
}
}
package com.my.suanfa;
import com.my.util.SingleNode;
public class Solution02 {
public SingleNode reversePart(SingleNode head, int from, int to) {
int len = 0;
SingleNode cur = head;
SingleNode fPre = null;
SingleNode tPos = null;
while(cur != null) {
len++;
fPre = (len == from - 1) ? cur : fPre;
tPos = (len == to + 1) ? cur : tPos;
cur = cur.next;
}
if(from < 1 || from > to || to > len) {
return head;
}
SingleNode pre = (fPre == null) ? head : fPre.next;
cur = pre.next;
pre.next = tPos;
SingleNode next = null;
while(cur != tPos) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
if(fPre != null) {
fPre.next = pre;
return head;
}
return pre;
}
}