注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
public ListNode reverseBetween(ListNode head, int m , int n) {
// write your code
if (m > n || head == null) {
return null;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
head = dummy;
int i = 0;
for (i = 1; i < m; i++) {
if (head == null) {
return null;
}
head = head.next;
}
ListNode preM = head;
ListNode Mnode = preM.next;
ListNode Nnode = Mnode;
ListNode postN = Nnode.next;
for (i = m; i < n; i++) {
ListNode temp = postN.next; // preM --> Mnode/Nnode --> postN --> (temp)
postN.next = Nnode; // preM postN --> Mnode/Nnode
Nnode = postN; // preM Nnode(postN) --> postN
postN = temp; // preM -- -- Nnode --> postN(temp)
}
preM.next = Nnode; //preM --> Nnode --> postN(temp)
Mnode.next = postN; // Mnode --> postN
return dummy.next;
}
}
|