C++ 反转单链表

#include 

typedef struct Node {
	Node(int value,Node* node) {
		this->next = node;
		this->value = value;
	}
	Node* next;
	int value;
}Node, *PNODE;

static Node* reverseList(Node* n) {
	Node* prev = nullptr;
	while (n) {
		Node* next = n->next;
		n->next = prev;
		prev = n;
		n = next;
	}
	n = prev;
	return n;
}

int main()
{
	Node* n5 = new Node(5,nullptr);
	Node* n4 = new Node(4,n5);
	Node* n3 = new Node(3,n4);
	Node* n2 = new Node(2,n3);
	Node* n1 = new Node(1,n2);
	Node* n0 = new Node(0,n1);
	n0 = reverseList(n0);

	return 0;
}

 

你可能感兴趣的:(other)