微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/javaschema/286920.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐