现在的位置: 首页 > 综合 > 正文

链表逆置

2013年06月02日 ⁄ 综合 ⁄ 共 769字 ⁄ 字号 评论关闭

单链表逆置

SingleList* SingleList::reverseList(SingleList *head){
	SingleList *tmp = head;
	if((head==NULL)||(head->next==NULL))return head;

	SingleList* pre = head->next;
	SingleList* cur = pre->next;
	SingleList* post = pre->next;
	while(cur->next!=NULL){
		post = post->next;
		cur->next = pre;
		pre = cur;
		cur = post;
	}
	head->next->next=NULL;
	head->next =cur;
	cur->next = pre;
	return head;
}

双链表逆置

DoublyLinkedList* DoublyLinkedList::reverseDLList(DoublyLinkedList* head){
	if((head==NULL)||(head->next()==NULL))return head;
	DoublyLinkedList* pre = head->next();
	DoublyLinkedList* cur = pre->next();
	DoublyLinkedList* post = pre->next();
	while(cur->next()!=NULL){
		post=post->next();
		cur->setNext(pre);
		cur->setPre(post);
		pre = cur;
		cur= post;
	}
	cur->setNext(pre);
	pre = head->next();
	pre->setPre(pre->next());
	pre->setNext(NULL);		
	head->setNext(cur);
	return head;
}

抱歉!评论已关闭.