单链表反转
2011-11-16 18:04
190 查看
#include <stdio.h>
#include <stdlib.h>
typedef struct _Node{
int data;
_Node* next;
}Node, *LinkList;
//一般反转
LinkList Reverse(LinkList &head)
{
if(head == NULL)
return NULL;
Node *pre, *cur, *next;
pre = head;
cur = head->next;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = NULL;
head = pre;
return head;
}
//递归反转
//注意最后返回值的next域置NULL
LinkList RReverse(LinkList p, LinkList &head)
{
if( (p==NULL) || (p->next==NULL) )
{
head = p;
return p;
}
else
{
LinkList tmp = RReverse(p->next, head);
tmp->next = p;
return p;
}
}
bool CreateList(LinkList &head, const int *data, const int len)
{
Node *cur = NULL;
Node *next = NULL;
int i;
cur = (LinkList)malloc(sizeof(Node));
if(cur == NULL)
return false;
cur->data = data[0];
cur->next = NULL;
head = cur;
for(i=1; i<len; i++)
{
next = (LinkList)malloc(sizeof(Node));
if(next == NULL)
return false;
next->data = data[i];
next->next = NULL;
cur->next = next;
cur = cur->next;
}
return true;
}
void PrintList(LinkList head)
{
while(head != NULL)
{
printf(" %d", head->data);
head = head->next;
}
}
void main( void )
{
int data[] = {1, 2, 3, 4, 5, 6};
int len = sizeof(data)/sizeof(int);
LinkList head;
if( !CreateList(head, data, len) )
{
printf("创建链表失败!\n");
return;
}
printf("反转前:");
PrintList(head);
printf("\n");
Reverse(head);
printf("反转后:");
PrintList(head);
printf("\n");
LinkList tail = RReverse(head, head);
tail->next = NULL;
//没有这条语句,则反转后的最后两个节点会形成环
printf("二次反转后:");
PrintList(head);
printf("\n");
}
#include <stdlib.h>
typedef struct _Node{
int data;
_Node* next;
}Node, *LinkList;
//一般反转
LinkList Reverse(LinkList &head)
{
if(head == NULL)
return NULL;
Node *pre, *cur, *next;
pre = head;
cur = head->next;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
head->next = NULL;
head = pre;
return head;
}
//递归反转
//注意最后返回值的next域置NULL
LinkList RReverse(LinkList p, LinkList &head)
{
if( (p==NULL) || (p->next==NULL) )
{
head = p;
return p;
}
else
{
LinkList tmp = RReverse(p->next, head);
tmp->next = p;
return p;
}
}
bool CreateList(LinkList &head, const int *data, const int len)
{
Node *cur = NULL;
Node *next = NULL;
int i;
cur = (LinkList)malloc(sizeof(Node));
if(cur == NULL)
return false;
cur->data = data[0];
cur->next = NULL;
head = cur;
for(i=1; i<len; i++)
{
next = (LinkList)malloc(sizeof(Node));
if(next == NULL)
return false;
next->data = data[i];
next->next = NULL;
cur->next = next;
cur = cur->next;
}
return true;
}
void PrintList(LinkList head)
{
while(head != NULL)
{
printf(" %d", head->data);
head = head->next;
}
}
void main( void )
{
int data[] = {1, 2, 3, 4, 5, 6};
int len = sizeof(data)/sizeof(int);
LinkList head;
if( !CreateList(head, data, len) )
{
printf("创建链表失败!\n");
return;
}
printf("反转前:");
PrintList(head);
printf("\n");
Reverse(head);
printf("反转后:");
PrintList(head);
printf("\n");
LinkList tail = RReverse(head, head);
tail->next = NULL;
//没有这条语句,则反转后的最后两个节点会形成环
printf("二次反转后:");
PrintList(head);
printf("\n");
}
相关文章推荐
- Leetcode Reverse Linked List 反转单链表
- 反转带头结点的链表
- 单链表反转/逆序的两种方法
- 【面试题16】反转链表
- PAT-乙级-1025. 反转链表 (25)
- 链表反转
- 16 反转链表
- [leetcode] Reverse Linked List II 链表反转
- 两两反转单向链表 (reverse a singly linked list in pair ) [#22]
- 反转链表
- 【面试题】剑指offer16--反转链表
- 题目1518:反转链表
- 递归和非递归实现链表反转
- 剑指offer——链表的递归反转打印
- 单链表中k个结点一组进行反转
- 反转链表
- leetcode-反转链表
- 剑指offer-第三章高质量代码(反转链表)
- C实现 LeetCode->Reverse Linked List II (双指针大法)(单链表反转)
- 反转链表