/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverse(ListNode *head)
{
if(head==NULL||head->next==NULL)
return head;
ListNode *pre=head;
ListNode *curr=head->next;
while(curr!=NULL)
{
ListNode *temp=curr->next;
curr->next=pre;
pre=curr;
curr=temp;
}
head->next=NULL;
return pre;
}
void reorderList(ListNode *head) {
if(head==NULL||head->next==NULL) return;
ListNode *pre=NULL;
ListNode *slow=head;
ListNode *fast=head;
while(fast&&fast->next)
{
pre=slow;
slow=slow->next;
fast=fast->next->next;
}
pre->next=NULL;
ListNode *slow2=reverse(slow);
ListNode *curr=head;
while(curr->next!=NULL)
{
ListNode *temp=curr->next;
curr->next=slow2;
slow2=slow2->next;
curr->next->next=temp;
curr=temp;
}
curr->next=slow2;
}
};
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *reverse(ListNode *head)
{
if(head==NULL||head->next==NULL)
return head;
ListNode *pre=head;
ListNode *curr=head->next;
while(curr!=NULL)
{
ListNode *temp=curr->next;
curr->next=pre;
pre=curr;
curr=temp;
}
head->next=NULL;
return pre;
}
void reorderList(ListNode *head) {
if(head==NULL||head->next==NULL) return;
ListNode *pre=NULL;
ListNode *slow=head;
ListNode *fast=head;
while(fast&&fast->next)
{
pre=slow;
slow=slow->next;
fast=fast->next->next;
}
pre->next=NULL;
ListNode *slow2=reverse(slow);
ListNode *curr=head;
while(curr->next!=NULL)
{
ListNode *temp=curr->next;
curr->next=slow2;
slow2=slow2->next;
curr->next->next=temp;
curr=temp;
}
curr->next=slow2;
}
};