# leetcode -day30 Reverse Linked List II

66 篇文章 0 订阅
1、
﻿﻿

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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
if(m < 1 || m >= n){
}
//双指针法找到要翻转的链表段
int dis = n - m;
int i = 0;
for(; i<dis && node2; ++i){
node2 = node2->next;
}
if(i<dis){
}
while(i<n-2 && node2){
node1 = node1->next;
node2 = node2->next;
++i;
}
//node3为要翻转的链表段的开始结点
ListNode* node3 = node1->next;
if(m == 1){
node1 = NULL;
}else{
node1->next = NULL;
node2 = node2->next;
if(!node2){
}
++i;
}
//node4为剩下的链表
ListNode* node4 = NULL;
node4 = node2->next;
node2->next = NULL;
//如果链表长度大于n时，可以进行
if(i == n-1){
//连接三段链表
node3->next = node4;
if(m !=1 ){
}else{
}
}
}
ListNode* node1 = NULL;
ListNode* tempNode = NULL;
while(node2){
tempNode = node2->next;
node2->next = node1;
node1 = node2;
node2 = tempNode;
}
return node1;
}
};

class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
return NULL;

ListNode *q = NULL;
for(int i = 0; i < m - 1; i++)
{
q = p;
p = p->next;
}

ListNode *end = p;
ListNode *pPre = p;
p = p->next;
for(int i = m + 1; i <= n; i++)
{
ListNode *pNext = p->next;

p->next = pPre;
pPre = p;
p = pNext;
}

end->next = p;
if (q)
q->next = pPre;
else

}
};

04-14 51
05-11 221
09-13 30
05-20 985
07-22 37
05-19 69
03-20 4058

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

xiao囡囡

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。