#include <stdio.h>
#include <malloc.h>
#define N 10010
int a[N];
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head){
printf("reListNode\n");
if (head == NULL || head->next == NULL)
return head;
struct ListNode *p = head->next, *q;
head->next = NULL;
while (p != NULL){
q = p->next;
p->next = head;
head = p;
p = q;
}
return head;
}
void OutListNode (struct ListNode *L){
struct ListNode *q;
q = L->next;
printf("OutListNode\n");
while(q != NULL){
printf("%d", q->val);
q = q->next;
}
}
struct ListNode* CreateList(struct ListNode *L, int a[], int n){
printf("CreateListNode\n");
struct ListNode* p = L->next;
for (int i = 0; i < n; i ++){
p->val = a[i];
p = p->next;
}
printf("cend");
return L;
}
void DestroyList(struct ListNode *L){
printf("555");
struct ListNode *q, *pre;
pre = L;
while (q != NULL){
q = pre->next;
free(pre);
}
}
int main() {
struct ListNode *L;
L = (struct ListNode *)malloc(sizeof(struct ListNode));
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", a+i);
CreateList(L, a[N], n);
L = reverseList(L);
OutListNode(L);
DestroyList(L);
return 0;
}
想实现一下链表逆置,为啥这个代码没有进Out里面啊?

结果是这么个玩意