Reverse Linked List
Tags: linkedlist, beginners, codenewbie, javascript
Published: 2022-04-15 16:25:56

Let’s practice [reversing a singly linked list](https://leetcode.com/problems/reverse-linked-list/) as this is a popular interview question!

> The problem is, given the head of a singly linked list, reverse it, and return the reversed list.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kwip8nqd13qzp8hg9cp.jpeg)

---

We can solve this problem by reordering the original list instead of creating a new one, and returning the tail node as the new head of the list.

If we’ll look at the first node, what do we know about it? We know that it has a pointer to the next node, and no nodes pointing to it. If we reverse its next pointer backwards immediately, we’ll orphan the rest of the list. In addition, we don't know which node should it point to. We need some helper cursors to keep track of the nodes! 

Thus, we will define three cursors: _curr_ to remember the current node, _prev_ to remember its previous node (firstly initialized to null), and _tmp_ to remember the next node (we’ll define it as a local variable inside the loop).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm3emdxr6ctsqp1r21f5.png)

```javascript
const reverseList = (head) => {
  let curr = head; //cursor to the current node
  let prev = null; //cursor to the previous node
...
}
```


Since we remember the next node, we can safely change the cursors. But the order is important!

First, we move our _curr_ pointer backwards to link it with the previous node (or null initially):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5rh192v8lc1csc7vx24.png)

```javascript
curr.next = prev;
```


Then _prev_ becomes our current node, and the current becomes the next node:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wacw6zfksljbul8skn3n.png)

```javascript
prev = curr;
curr = tmp;
```


That’s it, in the next iteration we can move to the next node and repeat the process.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f79fz9d11josfxjasw2.png)

We keep moving along the list unless we reach the end (the current cursor points to null). By the end of the cycle, we return a new head of the list, our prev node (not prev.next since prev.next is null).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y4rq1cmvf327c729d6xb.png)

```javascript
while (curr) { //curr !== null
    let tmp = curr.next; //move cursor to the next node

    curr.next = prev; //1.change curr pointer backwards
    prev = curr; //2.change prev cursor to current
    curr = tmp; //3.move cur cursor to the next node
  }
```


Let's put it all together.

```javascript
const reverseList = (head) => {
  let curr = head; //cursor to the current node
  let prev = null; //cursor to the previous node
  
  while (curr) { //curr !== null
    let tmp = curr.next; //move cursor to the next node

    curr.next = prev; //1.change curr pointer backwards
    prev = curr; //2.change prev cursor to current
    curr = tmp; //3.move cur cursor to the next node
  }
  return prev; //return a new head of the list
};
```

---

We can do it better with recursion! Know how? Please feel free to share your solution in the comments!