单向不循环链表就地转置算法示例:
A->B->C->D->E->F->G-> NULL
//这里至少需要定义三个临时变量
定义3个指针temp1, temp2, temp3
temp1=A,temp2=B,temp3=C;
temp1->next = NULL;
while(temp3 != NULL)
{
temp2->next = temp1;
temp1 = temp2;
temp2 = temp3;
temp3 = temp3->next;
}
temp2->next = temp1;
head = temp2;
===========将上面的算法封装成函数============
void *reverse(struct node_st *head)
{
struct node_st *p1, *p2 ,*p3;
if(head == NULL) return NULL;
if(head->next == NULL) return head;
p1 = head;
p2 = head->next;
p3 = head->next->next;
p1->next = NULL;
while(p3 != NULL){
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
head = p2;
return head;
}
===========另一种写法,大同小异============
Node* Reverse(Node* head)
{
//没有节点或只有一个节点
Node *p, *pre, *cur;
if (head == NULL )
return NULL;
if (head->next == NULL)
return head;
pre = p = head;
p = p->next;
head->next = NULL;
while ( p != NULL)
{
cur = p;
p = p->next;
cur->next = pre;
pre = cur;
}
head = cur;
return head;
}
=======================================
#include <stdio.h>
#include <stdlib.h>
struct node_st {
int id;
struct node_st *next;
};
void *reverse( struct node_st *head)
{
struct node_st *p1, *p2 ,*p3;
if(head == NULL) return NULL;
if(head->next == NULL) return head;
p1 = head;
p2 = head->next;
p3 = head->next->next;
p1->next = NULL;
while(p3 != NULL){
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
head = p2;
return head;
}
void *Reverse( struct node_st *head)
{
struct node_st *p, *pre, *cur;
if(head == NULL) return NULL;
if(head == NULL) return head;
pre = p = head;
p = p->next;
head->next = NULL;
while(p != NULL){
cur = p;
p = p->next;
cur->next = pre;
pre = cur;
}
head = cur;
return head;
}
int main( void)
{
int pers = 10;
struct node_st *p, *q, *tmp;
int i;
q = p = malloc(pers * sizeof( struct node_st));
if ( NULL == p) {
perror( "malloc()");
return - 1;
}
//创建单链表
tmp = p;
i = 1;
while (i < pers) {
tmp->id = i;
tmp->next = p + i;
tmp = tmp->next;
i++;
}
tmp->id = i;
tmp->next = NULL;
printf( "reverse\n");
p = reverse(p);
tmp = p;
while (tmp != NULL) {
printf( "id = %d\n", tmp->id);
tmp = tmp->next;
}
printf( "Reverse\n");
p = reverse(p);
tmp = p;
while (tmp != NULL) {
printf( "id = %d\n", tmp->id);
tmp = tmp->next;
}
free(q);
return 0;
}
author:elikang Linux && 技术分享群 311078264
A->B->C->D->E->F->G-> NULL
//这里至少需要定义三个临时变量
定义3个指针temp1, temp2, temp3
temp1=A,temp2=B,temp3=C;
temp1->next = NULL;
while(temp3 != NULL)
{
temp2->next = temp1;
temp1 = temp2;
temp2 = temp3;
temp3 = temp3->next;
}
temp2->next = temp1;
head = temp2;
===========将上面的算法封装成函数============
void *reverse(struct node_st *head)
{
struct node_st *p1, *p2 ,*p3;
if(head == NULL) return NULL;
if(head->next == NULL) return head;
p1 = head;
p2 = head->next;
p3 = head->next->next;
p1->next = NULL;
while(p3 != NULL){
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
head = p2;
return head;
}
===========另一种写法,大同小异============
Node* Reverse(Node* head)
{
//没有节点或只有一个节点
Node *p, *pre, *cur;
if (head == NULL )
return NULL;
if (head->next == NULL)
return head;
pre = p = head;
p = p->next;
head->next = NULL;
while ( p != NULL)
{
cur = p;
p = p->next;
cur->next = pre;
pre = cur;
}
head = cur;
return head;
}
=======================================
#include <stdio.h>
#include <stdlib.h>
struct node_st {
int id;
struct node_st *next;
};
void *reverse( struct node_st *head)
{
struct node_st *p1, *p2 ,*p3;
if(head == NULL) return NULL;
if(head->next == NULL) return head;
p1 = head;
p2 = head->next;
p3 = head->next->next;
p1->next = NULL;
while(p3 != NULL){
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
head = p2;
return head;
}
void *Reverse( struct node_st *head)
{
struct node_st *p, *pre, *cur;
if(head == NULL) return NULL;
if(head == NULL) return head;
pre = p = head;
p = p->next;
head->next = NULL;
while(p != NULL){
cur = p;
p = p->next;
cur->next = pre;
pre = cur;
}
head = cur;
return head;
}
int main( void)
{
int pers = 10;
struct node_st *p, *q, *tmp;
int i;
q = p = malloc(pers * sizeof( struct node_st));
if ( NULL == p) {
perror( "malloc()");
return - 1;
}
//创建单链表
tmp = p;
i = 1;
while (i < pers) {
tmp->id = i;
tmp->next = p + i;
tmp = tmp->next;
i++;
}
tmp->id = i;
tmp->next = NULL;
printf( "reverse\n");
p = reverse(p);
tmp = p;
while (tmp != NULL) {
printf( "id = %d\n", tmp->id);
tmp = tmp->next;
}
printf( "Reverse\n");
p = reverse(p);
tmp = p;
while (tmp != NULL) {
printf( "id = %d\n", tmp->id);
tmp = tmp->next;
}
free(q);
return 0;
}
author:elikang Linux && 技术分享群 311078264