首页 > 代码库 > 反转链表 --剑指offer
反转链表 --剑指offer
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反正后链表的头结点。
#include<stdio.h>#include<malloc.h>typedef struct node{ int Element; struct node *Link;}Node;Node *ReverseList(Node *first){ Node *NewFirst,*p; if(first==NULL||first->Link==NULL) { return first; } NewFirst=first; while(first->Link!=NULL) { p=first->Link; first->Link=p->Link; p->Link=NewFirst; NewFirst=p; } return NewFirst;}void main(){ int i=0; Node *p,*q,*first=NULL; while(i<10) { p=(Node*)malloc(sizeof(Node)); p->Element=i; if(!first) { first=p; first->Link=NULL; } else { p->Link=first; first=p; } i++; } q=first; while(q) { printf("%d,",q->Element); q=q->Link; } printf("\n"); p=ReverseList(first); while(p!=NULL) { printf("%d,",p->Element); p=p->Link; } }
反转链表 --剑指offer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。