-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate Linked List.java
More file actions
45 lines (41 loc) · 1.08 KB
/
Copy pathRotate Linked List.java
File metadata and controls
45 lines (41 loc) · 1.08 KB
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
44
45
import java.util.* ;
import java.io.*;
/***********************************************
Following is the class structure of the Node class:
class Node {
int data;
Node next;
Node(int x) {
this.data = x;
this.next = null;
}
}
************************************************/
public class Solution {
public static Node rotate(Node head, int k) {
// Write your code here.
int length = 0;
Node temp = head;
while(temp!=null && temp.data!=-1){
length++;
temp = temp.next;
}
int K = length -(k%length);
if(K==length){
return head;
}
//System.out.println(length);
Node current = head;
while(K-- > 1){
current = current.next;
}
Node start = current.next;
while(start!=null && start.next!=null && start.next.data!=-1){
start=start.next;
}
Node ans = current.next;
current.next = null;
start.next = head;
return ans;
}
}