Technology, life, programming, travel and money.

# 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* newList = NULL;
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;
// cleanup memory
while (current)
{
Node* next = current->next;
delete current;
current = next;
}
}
{
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.

#### 8 Responses to “Reverse a linked list – C++ source code”

1. meme says:

This is the most elegant way I’ve seen so far !
Thanks

2. dexter says:

+1

3. Hunter says:

de way u explained s simple and understandable.
You are awesome.
:-)))))

4. yj says:

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;
}

5. reviewmylife says:

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.

6. yj says:

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

7. ped says:

It seems you are writing C in C++ grammar.

8. HS says:

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.