title: time complexity of reversing a linked list
body: <p>I wrote the following function to reverse a linkedlist and was wondering about the time complexity of the 'swapping'. my rationale is as follows:
  it is o(n)</p>
  
  <p>insertion/deletion in a linkedlist is o(1), but that assumes just adding to the tail or removing from the head/tail. here you are iterating and accessing each element, and accessing elements in a linked list is o(n).
  is that correct? generally, what is the time complexity of swaps?</p>
  
  <pre><code>LinkedList.prototype.reverse = function () {
    var previous = null;
    var current = this.head;
    var next;
    while (current) {
      //swap pointers
      //cache iteration/temp var
      next = current.next;
      //point the next to the previous
      current.next = previous;
      //previous is the current one
      previous = current;
  
      //iterate
      current = next;
    }
    //after you're done set the head to null (ie make it the tail)
    this.head = null;
  };
  </code></pre>