package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 61. Rotate List
 *
 * Given a list, rotate_naive the list to the right by k places, where k is non-negative.

 For example:
 Given 1->2->3->4->5->NULL and k = 2,
 return 4->5->1->2->3->NULL.
 */
public class _61 {

  public static class Solution1 {
    //credit: https://discuss.leetcode.com/topic/26364/clean-java-solution-with-brief-explanation
    //link the tail of the linked list to the head to form a circle, then count to find the pint and cut it
    public ListNode rotateRight(ListNode head, int k) {
      if (head == null) {
        return head;
      }
      ListNode copyHead = head;
      int len = 1;
      while (copyHead.next != null) {
        copyHead = copyHead.next;
        len++;
      }
      copyHead.next = head;//link the tail and head to make it a circle
      for (int i = len - k % len; i > 1; i--) {
        head = head.next;
      }
      copyHead = head.next;
      head.next = null;//break the circle
      return copyHead;
    }
  }
}

@@ -82,210 +82,8 @@
 e;%0A%0A
-/**%0A * 61. Rotate List%0A *%0A * Given a list, rotate_naive the list to the right by k places, where k is non-negative.%0A%0A For example:%0A Given 1-%3E2-%3E3-%3E4-%3E5-%3ENULL and k = 2,%0A return 4-%3E5-%3E1-%3E2-%3E3-%3ENULL.%0A */%0A
 publ
@@ -98,16 +98,18 @@
  _61 %7B%0A%0A
+  
   public
@@ -138,18 +138,35 @@
 1 %7B%0A    
-//
+    /**%0A         * 
 credit: 
@@ -253,18 +253,23 @@
 ion%0A    
-//
+     * 
 link the
@@ -362,16 +362,32 @@
  cut it%0A
+         */%0A    
     publ
@@ -430,24 +430,30 @@
 d, int k) %7B%0A
+      
       if (he
@@ -470,24 +470,32 @@
 ) %7B%0A        
+    
+    
 return head;
@@ -491,24 +491,30 @@
 eturn head;%0A
+      
       %7D%0A    
@@ -515,16 +515,22 @@
 %7D%0A      
+ 
+     
 ListNode
@@ -543,24 +543,30 @@
 ead = head;%0A
+      
       int le
@@ -578,16 +578,22 @@
 ;%0A      
+ 
+     
 while (c
@@ -620,24 +620,32 @@
 ) %7B%0A        
+      
+  
 copyHead = c
@@ -654,24 +654,32 @@
 yHead.next;%0A
+        
         len+
@@ -679,34 +679,46 @@
    len++;%0A      
+ 
-%7D%0A
+     %7D%0A      
       copyHead.n
@@ -769,24 +769,30 @@
 it a circle%0A
+      
       for (i
@@ -823,24 +823,32 @@
 %3E 1; i--) %7B%0A
+        
         head
@@ -867,24 +867,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 copyHead
@@ -905,24 +905,30 @@
  head.next;%0A
+      
       head.n
@@ -959,24 +959,30 @@
 ircle%0A      
+ 
+     
 return copyH
@@ -990,16 +990,22 @@
 ad;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 61