Write JavaScript code to reverse a linked list
```js
function reverse(head) {
  let prev = null;
  let node = head;
  while (node) {
    // Save next, so we can proceed to the next node in the original list
    // After reversing the pointer.
    const next = node.next;

    // Reverse the pointer to point back at the previous node.
    node.next = prev;

    // Shift pointers ahead
    prev = node;
    node = next;
  }
  return prev;
}
```
Explanation:

We use prev to track the previous node as we traverse the list
We save the next node first before overwriting node.next, otherwise we would lose the link and get stuck
By reversing the pointer and shifting ahead in one loop, this is an efficient O(n) solution
Below is an instruction that describes a task. Write a response that appropriately completes the request.

### Instruction:

Write JavaScript code to reverse a linked list

### Response:

```js
function reverse(head) {
  let prev = null;
  let node = head;
  while (node) {
    // Save next, so we can proceed to the next node in the original list
    // After reversing the pointer.
    const next = node.next;

    // Reverse the pointer to point back at the previous node.
    node.next = prev;

    // Shift pointers ahead
    prev = node;
    node = next;
  }
  return prev;
}
```
Explanation:

We use prev to track the previous node as we traverse the list
We save the next node first before overwriting node.next, otherwise we would lose the link and get stuck
By reversing the pointer and shifting ahead in one loop, this is an efficient O(n) solution
