//============================================================================
// Name : C++链表题一个链表的结点结构.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
struct Node
{
int date;
Node*next;
Node(int Date):date(Date),next(NULL){}
};
typedef struct Node Node;
Node* ReverseList1(Node *head)
{
if(head==NULL||head->next==NULL)
return head;
Node*q=head->next;
Node*r=ReverseList1(head->next);
q->next=head;
head->next=NULL;
return r;
}
Node* ReverseList(Node *head)
{
if(head==NULL||head->next==NULL)
return head;
Node *p=head;
Node *q=NULL;
Node *r=NULL;
while(p)
{
r=p->next;
p->next=q;
q=p;
p=r;
}
return q;
}
int main() {
Node *head;
head=new Node(1);
head->next=new Node(2);
Node *p=head->next;
p->next=new Node(3);
p->next->next=new Node(4);
Node *temp= ReverseList1(head);
p=temp;
while(p)
{
cout<<p->date<<" ";
p=p->next;
}
return 0;
}
7646

被折叠的 条评论
为什么被折叠?



