#include <iostream>

#include "Node.h"

using namespace std;

void printList(Node * head);
Node* reverseList(Node * head);

int main()
{
Node * one = new Node(1);
Node * two = new Node(2);
Node * three = new Node(3);
Node * four = new Node(4);
Node * five = new Node(5);

one->setNext(two);
two->setNext(three);
three->setNext(four);
four->setNext(five);

//Test copy constructor - creates recursive list!
//Node * six(two);
//five->setNext(six);

Node * head = one;

printList(head);

head = reverseList(head);
printList(head);

//Cleanup memory
Node * current = head;
while(current)
{
    Node * next = current->myNextNode();
    delete current;
    current = next;
}

return 0;
}

void printList(Node * head)
{
Node * current = head;
while(current)
{
    cout << current->getValue(); cout << ", ";
    current = current->myNextNode();
}
cout << endl;
}

Node * reverseList(Node * head)
{
Node * newList = NULL;
Node* current = head;
while(current)
{
    Node * next = current->myNextNode();
    current->setNext(newList);
    newList = current;
    current = next;
}
return newList;
}
//Node.h

#ifndef NODE_H
#define NODE_H

class Node
{
public:
    Node();
    Node(int value) : value(value), next(0) {}
    Node(const Node &);
    virtual ~Node();
    void setValue(int value){ this->value = value; }
    int getValue() const { return value; }
    void setNext(Node * nextNode) { next = nextNode; }
    Node * myNextNode() const { return next; }
protected:
private:
    int value;
    Node * next;
};

#endif // NODE_H
//Node.cpp

#include "Node.h"

Node::Node()
{
//ctor
}


Node::Node(const Node & rhs)
{
*this = rhs;
}


Node::~Node()
{
//dtor
}
This code uses one of the simplest approaches to a linked list in C++ and was written using CodeBlocks (a
free C++ IDE which can be downloaded online). It demonstrates constructing a linked list from nodes and
reversing the linked list.
Linked List