70,023
社区成员




#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode* next;
};
struct ListNode* createlist();
struct ListNode* reverse(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 = reverse(head);
printlist(head);
return 0;
}
struct ListNode* createlist()
{
struct ListNode* head = NULL, * tail = NULL, * p = NULL;
int data;
scanf("%d", &data);
while (data != -1)
{
p = (struct ListNode*)malloc(sizeof(struct ListNode));
p->data = data;
p->next = NULL;
if (head == NULL)
head = p;
else
tail->next = p;
tail = p;
scanf("%d", &data);
}
return head;
}
struct ListNode* reverse(struct ListNode* head)
{
struct ListNode* ptr1, * ptr2, * heado = NULL, * tail = NULL;
int a[100], index = 0;
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
a[index++] = ptr1->data;
for (int i = index - 1; i >= 0; i--)
{
ptr2 = (struct ListNode*)malloc(sizeof(struct ListNode));
ptr2->data = a[i];
ptr2->next = NULL;
if (heado = NULL)
heado = ptr2;
else
tail->next = ptr2;
tail = ptr2;
}
return heado;
}
struct ListNode* reverse(struct ListNode* head)
{
struct ListNode* ptr1, * ptr2, * heado = NULL, * tail = NULL;
int a[100], index = 0;
for (ptr1 = head; ptr1 != NULL; ptr1 = ptr1->next)
a[index++] = ptr1->data;
for (int i = index - 1; i >= 0; i--)
{
ptr2 = (struct ListNode*)malloc(sizeof(struct ListNode));
ptr2->data = a[i];
ptr2->next = NULL;
//if (heado = NULL)
if (heado == NULL)
heado = ptr2;
else
tail->next = ptr2;
tail = ptr2;
}
return heado;
}