forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateList.java
More file actions
43 lines (35 loc) · 752 Bytes
/
RotateList.java
File metadata and controls
43 lines (35 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package LinkedLists;
/**
* Author - archit.s
* Date - 19/10/18
* Time - 9:54 AM
*/
public class RotateList {
public ListNode rotateRight(ListNode A, int B) {
int l = 0;
ListNode slow = A;
ListNode fast = A;
ListNode temp = A;
int count = 0;
while(temp!=null){
l++;
temp = temp.next;
}
B = B%l;
if(B == 0){
return A;
}
while(count<B){
fast = fast.next;
count++;
}
while(fast.next != null){
fast = fast.next;
slow = slow.next;
}
ListNode head = slow.next;
fast.next = A;
slow.next = null;
return head;
}
}