newegg的东西不想要了,据收没问题吧?# PDA - 掌中宝
a*2
1 楼
修修补补好几次才过了测试,写的又臭又长,谁能帮我改一下啊?
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( m > n || !head) return NULL;
ListNode* tail1 = (m>1?head:NULL);
ListNode* head2 = NULL;
ListNode* tail2 = NULL;
int i;
for( i = 1; i < m-1; i++)
{
if(!tail1)return NULL;
tail1=tail1->next;
}
ListNode* p1 = tail1?tail1->next:head;
ListNode* p2 = NULL;
ListNode* p3 = NULL;
for(i = m; i <= n; i++)
{
head2 = p1;
p2 = p1->next;
p1->next = p3;
if(!p3) tail2 = p1;
p3 = p1;
p1 = p2;
}
if(tail1)tail1->next = head2;
else head = head2;
if(tail2) tail2->next = p1;
return head;
}
};
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( m > n || !head) return NULL;
ListNode* tail1 = (m>1?head:NULL);
ListNode* head2 = NULL;
ListNode* tail2 = NULL;
int i;
for( i = 1; i < m-1; i++)
{
if(!tail1)return NULL;
tail1=tail1->next;
}
ListNode* p1 = tail1?tail1->next:head;
ListNode* p2 = NULL;
ListNode* p3 = NULL;
for(i = m; i <= n; i++)
{
head2 = p1;
p2 = p1->next;
p1->next = p3;
if(!p3) tail2 = p1;
p3 = p1;
p1 = p2;
}
if(tail1)tail1->next = head2;
else head = head2;
if(tail2) tail2->next = p1;
return head;
}
};