# Middle-题目77：92. Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.

struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
if (m == n || !head || !head->next)
int i = 1;
struct ListNode* mPtr = head, *nPtr = head;
for (struct ListNode* node = head; node; node = node->next, i++) {
if (i == m)
mPtr = node;
if (i == n)
nPtr = node;
}
if (mPtr == head) {
while (mPtr != nPtr) {
struct ListNode* temp = mPtr;
mPtr = mPtr->next;
temp->next = nPtr->next;
nPtr->next = temp;
}
return nPtr;
}
else {
struct ListNode* mPtrPrev = head;
while (mPtrPrev->next != mPtr)
mPtrPrev = mPtrPrev->next;
mPtrPrev->next = nPtr;
while (mPtr != nPtr) {
struct ListNode* temp = mPtr;
mPtr = mPtr->next;
temp->next = nPtr->next;
nPtr->next = temp;
}
}
}

0ms，100%
Cmershen的碎碎念：

• 本文已收录于以下专栏：

举报原因： 您举报文章：Middle-题目77：92. Reverse Linked List II 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)