全部代码
#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);
return 0;
}
struct ListNode *createlist(){
struct ListNode* head,*p,*tail;
head = (struct ListNode *)malloc(sizeof(struct ListNode *));
head->next = NULL;
head->data = 0;
tail = head;
int num;
while(1){
scanf("%d",&num);
if(num==-1) break;
p = (struct ListNode *)malloc(sizeof(struct ListNode *));
p->data = num;
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
struct ListNode *deleteeven( struct ListNode *head ){
struct ListNode* temp,*p;
temp = head;
while(temp->next !=NULL){
if((temp->next->data)%2==0){
p = temp->next;
temp->next = temp->next->next;
free(p);
}
else
temp = temp->next;
}
return head->next;
}
提交代码
struct ListNode *createlist(){
struct ListNode* head,*p,*tail;
head = (struct ListNode *)malloc(sizeof(struct ListNode *));
head->next = NULL;
head->data = 0;
tail = head;
int num;
while(1){
scanf("%d",&num);
if(num==-1) break;
p = (struct ListNode *)malloc(sizeof(struct ListNode *));
p->data = num;
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
struct ListNode *deleteeven( struct ListNode *head ){
struct ListNode* temp,*p;
temp = head;
while(temp->next !=NULL){
if((temp->next->data)%2==0){
p = temp->next;
temp->next = temp->next->next;
free(p);
}
else
temp = temp->next;
}
return head->next;
}