第7题 倒置一个链表

题目:利用递归倒置一个链表


此题非常常见,因为很多公司在出面试题的时候,会考察面试人员的数据结构知识和算法知识,而有关链表的题是最具代表性的了。


这种题目不是非常难,适合做面试题,但又不简单,如果不提前做好准备,真正到了面试时,很难能做出来


#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<assert.h> struct node { int data; struct node* next; }; // build the list {1,2,3,4} in the heap and store its head pointer in a local // stack variable. // Returns the head pointer to the caller struct node* BuildOneTwoThreeFour(); // there is a short and efficient recursive solution to this problem. void RecursiveReverse(struct node** headRef); // print the element in the linked list in order void PrintLinkedList(struct node* head); struct node* BuildOneTwoThreeFour() { struct node* head = NULL; struct node* second = NULL; struct node* third = NULL; struct node* forth = NULL; head = malloc(sizeof(struct node)); second = malloc(sizeof(struct node)); third = malloc(sizeof(struct node)); forth = malloc(sizeof(struct node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = forth; forth->data = 4; forth->next = NULL; return head; } void RecursiveReverse(struct node** headRef) { struct node* first; struct node* rest; if(*headRef == NULL) return; first = *headRef; rest = first->next; if(rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headRef = rest; } void PrintLinkedList(struct node* head) { while(head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { struct node* head = BuildOneTwoThreeFour(); PrintLinkedList(head); RecursiveReverse(&head); PrintLinkedList(head); }


上面这个程序实现了链表的倒置,那个RecursiveReverse函数的内部指针变化,需要花时间去理解。先前,我一直不理解,有一次上课,不想听老师讲课,就把这个程序拿出来看了又看,用了2个小时时间,最后用gdb跟踪调试才搞明白指针的指向,这个方法非常的巧妙,一旦分析出来,就彻底记住了。


所以,一定要花时间分析这个程序,不然很容易就会忘了它的递归思路。



关于链表其实有非常多的面试题,这个倒置链表只是其中非常常见的例子,我近期会更新一些关于链表的文章,主要参考了stanford cs library的资料,对链表做出详尽的分析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值