首页 > 代码库 > 【C/C++】反转链表

【C/C++】反转链表

/*
 * reverseList.c
 *
 *  Created on: 2014年6月25日
 *      Author: wenbo
 */

#include <iostream>
using namespace std;
struct Node {
	Node* next;
	int data;
};
void trace(Node* node) {
	Node* tmp = node;
	while (tmp != NULL) {
		cout << tmp->data << " ";
		tmp = tmp->next;
	}
	cout<<endl;
}
Node* reverse(Node* node) {
	Node* first = node;
	Node* second = node->next;
	Node* tmp = second->next;
	Node* newHead;
	<span style="color:#ff0000;">first->next = NULL;//set head node as the last node</span>
	while (second != NULL) {
		second->next = first;
		first = second;
		second = tmp;
		if(tmp!=NULL){
			newHead = tmp;
			tmp = tmp->next;
		}
	}
	return newHead;
}
int main() {
	Node* node5 = new Node();
	node5->data = http://www.mamicode.com/5;>