diff --git a/Day7/Solution2.java b/Day7/Solution2.java new file mode 100644 index 0000000..b346cdb --- /dev/null +++ b/Day7/Solution2.java @@ -0,0 +1,30 @@ +class Solution { + public ListNode rotateRight(ListNode head, int k) { + if( head == null || head.next == null || k == 0 ) + return head; + + ListNode tail = null; + int n = 0; + + for( tail = head; tail.next != null; tail = tail.next ) + { + n++; + } + + n++; + k = k % n; + if(k == 0) + return head; + + ListNode cur = head; + for(int i = 1; i < n-k; i++) + { + cur = cur.next; + } + ListNode nHead = cur.next; + cur.next = null; + tail.next = head; + + return nHead; + } +}