#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *createlist();
struct ListNode *deleteeven(struct ListNode *head);
void printlist(struct ListNode *head)
{
struct ListNode *p = head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *head;
head = createlist();
head = deleteeven(head);
printlist(head);
system("pause");
return 0;
}
/* 你的代码将被嵌在这里 */
struct ListNode *createlist() {
struct ListNode *head = NULL, *tail = NULL, *p;
int num;
scanf("%d", &num);
while (num != -1) {
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = num;
if (head == NULL) head = tail = p;
else {
tail->next = p;
tail = p;
}
scanf("%d", &num);
}
tail->next = NULL;
return head;
}
struct ListNode *deleteeven(struct ListNode *head) {
struct ListNode *tail, *p;
while (head&&head->data % 2 == 0) {
p = head;
head = head->next;
free(p);
}
tail = head;
p = tail->next;
while (p != NULL) {
if (p->data % 2 == 0) {
tail->next = p->next;
free(p);
}
else tail = p;
p = tail->next;
}
return head;
}