#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode {
int val;
struct ListNode *next;
}ListNode;
ListNode * link_list_create_from_tail(int *data,int length)
{
ListNode *head = (ListNode *)malloc(sizeof(ListNode));
if(head == NULL){
printf("申请空间失败");
return NULL;
}
head->next = NULL;
ListNode *p = head;
for(int i=0;i<length;i++){
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
node->val = data[i];
node->next = NULL;
p->next = node;
p = node;
}
return head->next;
}
struct ListNode* reverseList(struct ListNode* head){
struct ListNode *prev = NULL;
struct ListNode *curr = head;
while(curr){
struct ListNode *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
void print_linkedlist(ListNode * head)
{
if(head == NULL)
{
printf("单链表为空");
}
ListNode *p = head;
while(p)
{
printf("%d ",p->val);
p = p->next;
}
}
int main(int argc,char *argv[])
{
int data1[2] = {1,6};
ListNode *tail1 = link_list_create_from_tail(data1,2);
print_linkedlist(tail1);
printf("\n");
ListNode *head = reverseList(tail1);
print_linkedlist(head);
return 0;
}