2013年百度实习生面试 编程题之--单链表的反转
2013-05-16 21:58
274 查看
单链表的反转,虽然想起来容易,但是要实现一个没有错误的可运行代码却没那么简单。代码如下
#include<stdio.h> #include<stdlib.h> typedef struct{ int value; struct node_t* next; }node_t; node_t* head; void buildLink(int); void printLink(); void buildLink(int n){ int i=0; node_t* tmp; node_t* tmpnode; if(n>=1){ head = malloc(sizeof(node_t)); head->value = rand()%99; tmp = head; for(i=1;i<n;i++){ tmpnode = malloc(sizeof(node_t)); tmpnode->value = rand()%99; // printf("value of node %d\n",i); // scanf("%d",&tmpnode->value); tmp->next = tmpnode; tmp = tmp->next; } } } void printLink(){ int i =0; node_t* first = head; while(first!=NULL){ printf("node %d 's value is %d\n ",i,first->value); first=first->next; i++; } } void revirse(){ node_t* first = head; node_t* tmp; node_t* afterTmp; if(head==NULL) return; tmp = first->next; if(tmp==NULL) return; afterTmp = tmp->next; while(afterTmp!=NULL){ tmp->next = first; first = tmp; tmp = afterTmp; afterTmp = afterTmp->next; } tmp->next = first; head->next =NULL; head = tmp; } int main(void){ int i =0; printf("input node number\n"); scanf("%d",&i); buildLink(i); printf("before revirse\n"); printLink(); revirse(); printf("after revirse\n"); printLink(); return 1; }
相关文章推荐
- 百度2017暑期实习生编程题
- 2017暑期实习生面试--百度,freewheel
- 百度实习生内推面试
- 阿里巴巴、百度前端工程师 2015 暑期实习生面试经历
- 我的百度实习生面试(一)
- 【百度、腾讯、阿里等】+【JAVA开发实习生】+春招面试经验
- 百度2017暑期实习生编程题
- 【百度】C++研发工程师实习生面试
- 2012年百度实习生笔试面试
- 面试题目之单链表的反转
- 记2012年百度web前端研发实习生的笔试面试经历
- 百度实习生电话面试经过与收获
- 百度2017暑期实习生编程题
- 面试百度新浪头条b站阿里的研发实习生面试记录
- 2016届校招 百度WEB前端研发【实习生+提前批】面试经历
- 百度数据挖掘实习生面试经验
- [综合面试] 2014校招暑假实习生面经(阿里、百度offer)
- [综合面试] 2014校招暑假实习生面经(阿里、百度offer)
- 页面调度算法----百度2017暑期实习生编程题
- 百度面试_Android(实习生)