Reverse a linked list – C++ source code

Reversing a linked list is a simple programming problem, which is often an interview question. In this case I’m referring to a singly linked list. I’ll provide the C++ solution and the C++ test code.

One of the easiest ways to reverse the linked list is to create a new head pointer, iterate through the list removing each item in turn and then inserting them at the beginning of the new list.

Here is the definition of my linked list node and the code to reverse the list.

class Node
{
public:
	Node(int value) : value(value), next(NULL) {}
public:
	int value;
	Node* next;
};


Node* reverseList(Node* head)
{
	Node* newList = NULL;
	Node* current = head;
	while (current)
	{
		Node* next = current->next;
		current->next = newList;

		newList = current;
		current = next;
	}
	return newList;
}

Below is the code I used to test this. This is what it does:

  1. It creates a simple linked list.
  2. Prints it out – output should be ‘12345’.
  3. Reverses it.
  4. Prints it out – output should be ‘54321’.
  5. Reverses it again.
  6. Prints it – output should be ‘12345’.
  7. Deletes the list.
void listTests()
{
	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->next = two;
	two->next = three;
	three->next = four;
	four->next = five;

	Node* head = one;

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

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

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

void printList(Node* head)
{
	Node* current = head;
	while (current)
	{
		cout << current->value;
		current = current->next;
	}
	cout << endl;
}

This should be the output:

12345
54321
12345

If you are interested in other linked list related code I have a post about picking the 5th from last element from a singly linked list, and one on selecting random numbers from a stream or linked list. Both of these are based on programming interview questions.

9 Comments on “Reverse a linked list – C++ source code”

  1. Sorry i do not understand this part, how does this select each item and put them into the newList? this while loop will run till current==NULL? right?

    while (current)
    {
    Node* next = current->next;
    current->next = newList;
    newList = current;
    current = next;
    }

  2. Hi yj, this code moves the nodes from one list to another by changing the next pointers. The easiest way to understand it is to get a piece of paper and work through the loop for the 1,2,3,4,5 example above.

    Yes it stops when current==NULL.

  3. hey, i got it thanks.. i was thinking of another way which may be easier. It works when you have both the prev and next pointer for each Node object. So to reverse the order, just interchange the next and prev pointers for each Node obj..Anyway thanks for sharing..Cheers

  4. I never see this assign statement before Node* next = current->next, but I’ve seen like this Node* temp-next = current->next;

    Can you explain little bit?

  5. Hi everyone I have question I liked this code but there is a some lines that I did not get what it do and when I tried to delete this line from the compiler it did not compile so can you explain to me what this do
    Node(int value) : value(value), next(NULL) {}
    I studied that ( : ) use for inheritance between tow class sorry I did not get it what is value(value) and next(NULL)
    And then in the next lines the node val and the pointer initialized
    public:
    int value;
    Node* next;
    };

    so what is this class please explain.

Leave a Reply

Your email address will not be published. Required fields are marked *

Do NOT fill this !