struct ListNode *readlist() {
struct ListNode *head = NULL, *tail = NULL, *newNode;
int num;
while (1) {
scanf("%d", &num);
if (num == -1) {
break;
}
newNode = (struct ListNode *)malloc(sizeof(struct ListNode));
newNode->data = num;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
struct ListNode *getodd(struct ListNode **L) {
struct ListNode *oddHead = NULL, *oddTail = NULL, *cur = *L, *prev = NULL;
while (cur) {
if (cur->data % 2 != 0) {
if (oddHead == NULL) {
oddHead = cur;
oddTail = cur;
} else {
oddTail->next = cur;
oddTail = cur;
}
if (prev == NULL) {
*L = cur->next;
cur = cur->next;
} else {
prev->next = cur->next;
cur = cur->next;
}
oddTail->next = NULL;
} else {
prev = cur;
cur = cur->next;
}
}
return oddHead;
}
1008611
于 2025-03-17 20:57:33 首次发布