## DEV Community is a community of 700,142 amazing developers

We're a place where coders share, stay up-to-date and grow their careers. # How to reverse a Singly Linked List Ricardo Borges

In a Singly Linked List, each node has only a pointer to the next node, and there are some ways to reverse it, the one that I want to show here doesn't require extra space.

In this algorithm, we start from the list's head, and we use three pointers, each one for previous node, current node and next node.
In the beginning, The current and the next points to the list's head, and the previous points to null. While the current are pointing to a node we do the following:

1. next point to its next node
2. current.next points to previous node
3. previous node points to the current node
4. current node points to next node

This loop continues until the current points to null, which will mean that we reached the end of the list. Like this:

``````prev = null;

while (current !== null) {
next = next.next;
current.next = prev;
prev = current;
current = next;
}
``````

A single iteration works like this: This algorithm may vary according to the Linked List implementation, using this implementation, looks like this:

``````function reverse(linkedList: LinkedList<number>) {
let prev = null;

while (current !== null) {
next = next.next;
current.next = prev;
prev = current;
current = next;
}

}

const linkedList = new LinkedList((a: number, b: number) => a === b);