node* reverse1(node * head)
{
node*p,*q,*r;
p = head;
q = head->next;
head->next = NULL;
while(q){
r = q->next;
q->next = p;
p = q;
q = r;
}
head=p;
/* while(p)
{
printf("%d ",p->num);
p=p->next;
}*/
printf("\n");
return head;
}
C语言中单向的链表反转
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- qq1223386926 2015-09-28 21:33关注
#include <stdio.h> #include <stdlib.h> typedef struct _node { int data; struct _node *next; } node; node* reverse1(node * head) { node*p,*q,*r; if( head != NULL ) { p = head; q = head->next; head->next = NULL; while(q){ r = q->next; q->next = p; p = q; q = r; } head=p; } return head; } int main( void ) { node *link = NULL; node *tmp = NULL; int i = 0; for( i = 0; i < 5; i++ ) { tmp = malloc( sizeof( node ) ); if( tmp == NULL ) return -1; tmp->data = i; tmp->next = link; link = tmp; } tmp = link; printf( "before reversal:\n" ); while( tmp ) { printf( "%d ", tmp->data ); tmp = tmp->next; } putchar( '\n' ); link = reverse1( link ); printf( "after reversal:\n" ); tmp = link; while( tmp ) { printf( "%d ", tmp->data ); tmp = tmp->next; } putchar( '\n' ); return 0; } 测了一下,应该可以使吧
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报