title: Reverse right half of a linked list
body: <blockquote>
    <p>reverse second half linkedlist<br>
    example:<br>
    even number:
    2->1->3->4->5->6->7->8 =====> 2->1->3->4->8->7->6->5 ；  </p>
    
    <p>odd number: 5->7->8->6->3->4->2 ======> 5->7->8->2->4->3->6, the
    middle one also need be reversed</p>
  </blockquote>
  
  <pre><code>class ListNode
  {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
  
  
  class ReverseRightHalfLinkedList 
  {
      public static void main(String[] args) 
      {       
          ListNode node1 = new ListNode(1);
          ListNode node2 = new ListNode(2);
          ListNode node3 = new ListNode(3);
          ListNode node4 = new ListNode(4);
          ListNode node5 = new ListNode(5);
          node1.next = node2;
          node2.next = node3;
          node3.next = node4;
          node4.next = node5;
  
          ListNode res = reverse(node1);//line 31
  
  //      ListNode node = node1;
  //      while (node != null)
  //      {
  //          System.out.println(node.val);
  //          node = node.next;
  //      }
  
      }
  
      public static ListNode reverse(ListNode start)
      {   
          int counter = 0;
          ListNode node = start;
          ListNode pre = start;
  
          while (node!= null)
          {
              counter += 1;
              node = node.next;           
          }
  
          for (int i=0; i&lt;counter/2; i++)
          {   
              pre = start;
              start = start.next; 
          }
  
          ListNode cur = start;
  
          if (counter%2 ==0)
          {
              while (cur != null)
              {
                  ListNode temp = cur.next;
                  cur.next = pre;
                  pre = cur;
                  cur = temp;
              }
          }
          else 
          {
              pre = pre.next;
              cur = start.next;
  
              System.out.println(pre.val);
              System.out.println(cur.val);
  
              while (cur != null)
              {
                  ListNode temp = cur.next;
                  cur.next = pre;
                  pre = cur;
                  cur = temp;
  
  
                  System.out.println("-----");
                  System.out.println(pre.val); // line 90
                  System.out.println(cur.val);
                  System.out.println("-----");
                  System.out.println();
              }
          }
  
          return start;
  
      }
  }
  </code></pre>
  
  <p>Firstly, I got an error message.</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException     at
    ReverseRightHalfLinkedList.reverse(OA2.java:90)   at
    ReverseRightHalfLinkedList.main(OA2.java:31)</p>
  </blockquote>
  
  <p>Secondly, I tried to print the order of reversed linked list, it is still in order. It was not reversed. </p>
  
  <p>Please help me to figure out these two problems. Thanks a lot!</p>