Reverse Linked List

Linked list construction is covered here

Example:

Linked list: [1,2,3,4,5]
Output: [5,4,3,2,1]
Linked List: []
Output: []

Solution:

var reverse = function (head) {
  var prev = null
  while (head != null) {
    var temp = head.next
    head.next = prev
    prev = head
    head = temp
  }
  return prev
}

Written by Gagandeep Rangi who likes to talk about himself in third person. Twitter Instagram