#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :val(x), next(NULL) {}
};
void reverse(ListNode* head);
int main()
{
int t = 0, n = 0;
ListNode *head1 = new ListNode(0);
ListNode *l1 = head1;
cin >> t;
while (t--)
{
int temp;
cin >> temp;
l1 = (l1->next = new ListNode(temp));
}
reverse(head1->next);
head1 = head1->next;
while (head1 != NULL) {
cout << head1->val << " ";
head1 = head1->next;
}
system("pause");
return 0;
}
void reverse(ListNode* head) {
ListNode* reverseList(ListNode* head);
if (head == NULL || head->next == NULL) return;
ListNode *fastp = head, *lowp = head, *tail = NULL;
while (fastp != NULL && fastp->next != NULL)
{
tail = lowp;
fastp = fastp->next->next;
lowp = lowp->next;
}
tail->next = NULL;
if (lowp == NULL || lowp->next == NULL) lowp=lowp;
ListNode *p = lowp, *pre = NULL;
while (p != NULL)
{
ListNode *tmp = p->next;
p->next = pre;
pre = p;
p = tmp;
}
lowp = pre;
fastp = head;
tail = NULL;
while (fastp != NULL)
{
ListNode*tp = fastp->next;
fastp->next = lowp;
ListNode*tr = lowp->next;
if (tp == NULL) break;
lowp->next = tp;
fastp = tp;
lowp = tr;
}
}
leetcode 143. Reorder List
最新推荐文章于 2022-10-08 18:49:12 发布