From fc67de38239c2fd3b67dca9ca255f7d1cc8cacd2 Mon Sep 17 00:00:00 2001 From: Chanpreet Singh <34856246+chanpreet1999@users.noreply.github.com> Date: Tue, 13 Oct 2020 02:08:12 +0530 Subject: [PATCH] Create Solution2.java --- Day7/Solution2.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Day7/Solution2.java 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; + } +}