public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null)
return null;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode preNode = dummy;
int i=1;
while(preNode.next!=null && i<m)
{
preNode = preNode.next;
i++;
}
if(i<m)
return head;
ListNode mNode = preNode.next;
ListNode cur = mNode.next;
while(cur!=null && i<n)
{
ListNode next = cur.next;
cur.next = preNode.next;
preNode.next = cur;
mNode.next = next;
cur = next;
i++;
}
return dummy.next;
}上面的代码还是有些细节的,链表的题目就是这样,想起来道理很简单,实现中可能会出些小差错,还是熟能生巧哈。Reverse Linked List II -- LeetCode
原文地址:http://blog.csdn.net/linhuanmars/article/details/24613781