金山(Kingsoft)笔试面试题整理之003

题目3:将单链表逆序。

#include 

typedef struct LNode {
	int data;
	struct LNode *next;
} LNode, List;

/*单链表逆序,假设单链表带头结点*/
void reverse(List L) {
	if (NULL == L || NULL == L -> next) {
		return;
	}
	LNode *p = L -> next -> next;
	LNode *q;
	while (p != NULL){
		q = p;
		p = p -> next;
		q -> next = L -> next;
		L -> next = q;
	}
}	

/*创建单链表,带头结点*/



你可能感兴趣的:(金山(Kingsoft)笔试面试题整理之003)