加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 程序设计 > 正文

第7题 倒置一个链表

发布时间:2020-05-23 12:08:16 所属栏目:程序设计 来源:互联网
导读:题目:利用递归倒置一个链表 此题非常常见,因为很多公司在出面试题的时候,会考察面试人员的数据结构知识和算法知识,而有关链表的题是最具代表性的了。 这种题目不是非常难,适合做面试题,但又不简单,如果不提前做好准备,真正到了面试时,很难能做出来 #

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


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


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


#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的资料,对链表做出详尽的分析。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读