#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct ListNode
{
int val;
struct ListNode *next;
} ListNode;
ListNode *verse(ListNode *p, int m, int n);
int main()
{
int m, n;
while (scanf("%d %d", &m, &n) != EOF)
{
ListNode *head, *p, *temp;
head = (ListNode *)malloc(sizeof(ListNode));
head->val = 1;
head->next = NULL;
p = head;
for (int i = 1; i < 10; i++)
{
temp = (ListNode *)malloc(sizeof(ListNode));
temp->val = i + 1;
temp->next = NULL;
p->next = temp;
p = temp;
}
p = head;
p = verse(p, m, n);
for (int i = 0; i < 10; i++)
{
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
return 0;
}
ListNode *verse(ListNode *p, int m, int n)
{
ListNode *start1 = NULL, *start2 = NULL, *end1 = NULL, *end2 = NULL;
ListNode *cur, *pre, *next;
cur = p;
for (int i = 0; i < m - 1; i++)
{
start1 = cur;
cur = cur->next;
}
start2 = cur;
for (int i = m - 1; i < n; i++)
{
end1 = cur;
cur = cur->next;
}
end2 = cur;
//记录第m-1,m,n,n+1个节点
if (start1)
start1->next = end1;
//从第m+1个开始翻转
cur = start2->next;
if (end2)
start2->next = end2;
pre = start2;
for (int i = m + 1; i <= n; i++)
{
//翻转
next = cur->next;
cur->next = pre;
//后移
pre = cur;
cur = next;
}
if(!start1)
return pre;
return p;
};