struct ListNode *readlist()
{
struct ListNode *p, *head=NULL, *tail;
int n;
scanf("%d", &n);
while (n != -1){
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = n;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
scanf("%d", &n);
}
return head;
}
struct ListNode *getodd (struct ListNode **L)
{
struct ListNode *tail, *p=*L, *head=NULL, *tail_2;
*L=NULL;
while (p != NULL){
if (p->data %2 == 0){ // 偶数直接存回 L中
if (*L == NULL)
*L = p;
else
tail->next = p;
tail = p; // 注意这三步
p = p->next; // 顺序不能反!!
tail->next = NULL; // 必须等 p变成下一个结点时,tail才能指 NULL
}
else {
if (head == NULL) // 奇数存到新链表中
head = p;
else
tail_2->next = p;
tail_2 = p;
p = p->next;
tail_2->next = NULL;
}
}
return head;
}