#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct _ListNode{
int data;
struct _ListNode *next;
}ListNode;
ListNode* LinkList_Create(int *value,int len)
{
ListNode *pHead = (ListNode*)malloc(sizeof(ListNode));
ListNode *pTail = pHead;
for(int i=0; i < len; i++)
{
ListNode *pCur = (ListNode*)malloc(sizeof(ListNode)) ;
pCur->data = value[i];
pTail->next = pCur;
pTail = pCur;
}
pTail->next = NULL;
return pHead;
}
void ShowNode(ListNode* head)
{
ListNode* p = head->next;
for(;p;p = p->next) printf("%d ",p->data);
printf("\n");
}
ListNode* ReverseList(ListNode* pHead)
{
ListNode *p=NULL;
ListNode *t = pHead->next;
ListNode *q = t->next;
if((pHead == NULL) || (q == NULL))
{
return NULL;
}
while(q)
{
t->next = p;
p = t;
t = q;
q = q->next;
}
t->next = p;
pHead->next = t;
return pHead;
}
int main()
{
int data[7] = {2,3,6,4,5,7,9};
ListNode* pHead = LinkList_Create(data,7);
ShowNode(pHead);
ReverseList(pHead);
ShowNode(pHead);
return 0;
}
链表反转
最新推荐文章于 2021-06-08 23:47:11 发布