// ConsoleApplication4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
typedef struct Node{
int value;
struct Node* next;
}TreeNode;
TreeNode *reverse(TreeNode *node,TreeNode *head)
{
if (node->next == NULL)
{
head = node;
return node;
}
TreeNode *p = reverse(node->next,head);
p->next = node;
node->next = NULL;
return node;
}
int _tmain(int argc, _TCHAR* argv[])
{
TreeNode *l,*head;
TreeNode *p;
l = (TreeNode *)malloc(sizeof(TreeNode));
l->value = 1;
head = l;
p = (TreeNode *)malloc(sizeof(TreeNode));
p->value = 2;
l->next = p;
l = p;
p = (TreeNode *)malloc(sizeof(TreeNode));
p->value = 3;
l->next = p;
l = p;
p = (TreeNode *)malloc(sizeof(TreeNode));
p->value = 4;
l->next = p;
l = p;
p = (TreeNode *)malloc(sizeof(TreeNode));
p->value = 5;
l->next = p;
l = p;
p = (TreeNode *)malloc(sizeof(TreeNode));
p->value = 6;
l->next = p;
l = p;
l->next = NULL;
p = reverse(head,l);
return 0;
}
递归实现链表反转
最新推荐文章于 2023-10-25 11:37:11 发布