title: reverse linked list in between 2 indices
body: <p>I'm trying to solve this problem and there's a bug in my code, but i can't figure out why: <a href="https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/" rel="nofollow noreferrer">https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/</a></p>
  
  <p>Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.</p>
  
  <p>For example,
  Given 1->2->3->3->4->4->5, return 1->2->5.
  Given 1->1->1->2->3, return 2->3.</p>
  
  <p>The error is </p>
  
  <pre><code>Line 44: TypeError: Cannot read property 'val' of null
  </code></pre>
  
  <p>Here is the code in Javascript</p>
  
  <pre><code>function ListNode(val) {
      this.val = val;
      this.next = null;
  }
  
  var reverseBetween = function(head, m, n) {
      if(m&gt;=n) return head;
      var currentNode = head;
      var previousNode = null;
      var temp = null, tempStart = null;
      var countm = 0, countn=0;
      if(!currentNode.next) return head;
      var startNode = null;
      console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
  
      while(countm&lt;m){
          //advance pointer and do nothing
          startNode = previousNode;
          previousNode = currentNode;
          currentNode = currentNode.next;
          countm++;
          console.log(previousNode.val + ', ' + currentNode.val)
      }
      console.log('before: startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
      while(countn &lt; (n-m)){
  
          temp = currentNode.next;
          currentNode.next = previousNode;
          startNode.next = currentNode;
          previousNode.next = temp;
  
  
          countn++;
          console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
          console.log([head.val, head.next.val, head.next.next.val, head.next.next.next.val, head.next.next.next.next.val]);
  
          //advance
          previousNode = currentNode;
          currentNode = currentNode.next;
          console.log('startNode: ' + startNode.val + ', prev: ' + previousNode.val + ', curr: ' +currentNode.val)
      }
  
      return head;
  };
  
  var a = new ListNode(1);
  var b = new ListNode(2);
  var c = new ListNode(3);
  var d = new ListNode(4);
  var e = new ListNode(5);
  a.next = b;
  b.next = c;
  c.next = d;
  d.next = e;
  
  reverseBetween(a, 2, 4)
  </code></pre>