1
+ import java .util .*;
2
+
3
+ /**
4
+ * Definition for singly-linked list.
5
+ **/
6
+ class SinglyLinkedNode {
7
+ int val ;
8
+ SinglyLinkedNode next ;
9
+
10
+ public SinglyLinkedNode (int val ) {
11
+ this .val = val ;
12
+ }
13
+ }
14
+
15
+ /**
16
+ * This is a program to demo rotating a singly linked list.
17
+ * @see <a href=
18
+ * "https://leetcode.com/problems/rotate-list/description/">LeetCode
19
+ * description</a>
20
+ * Time complexity O(n), Space O(1)
21
+ */
22
+ public class RotateLinkedList {
23
+ public static void main (String [] args ) {
24
+ try (Scanner sc = new Scanner (System .in )) {
25
+ System .out .println ("Enter the LinkedList in the form of [1,2,3,4,5]" );
26
+ String input = sc .nextLine ();
27
+ System .out .println ("Enter the number of rotations" );
28
+ int k = sc .nextInt ();
29
+ String str = input .replaceAll (" " , "" ) .substring (1 , input .length () - 1 );
30
+ String [] nodesStr = str .split ("," );
31
+ if (nodesStr .length == 0 ) {
32
+ System .out .println ("[]" );
33
+ return ;
34
+ }
35
+
36
+ SinglyLinkedNode head = buildLinkedList (nodesStr );
37
+ RotateLinkedList solution = new RotateLinkedList ();
38
+ SinglyLinkedNode newHead = solution .rotateRight (head , k );
39
+ printOutput (newHead );
40
+ } catch (Exception e ) {
41
+ System .out .println (
42
+ "Something went wrong. Check the linkedlist format is in the form of [1,2,3,4,5] and rotation is an integer" );
43
+ }
44
+ }
45
+
46
+ private static SinglyLinkedNode buildLinkedList (String [] nodesStr ) {
47
+ SinglyLinkedNode head = null ;
48
+ SinglyLinkedNode node = null ;
49
+ for (int i = 0 ; i < nodesStr .length ; i ++) {
50
+ SinglyLinkedNode newNode = new SinglyLinkedNode (Integer .valueOf (nodesStr [i ]));
51
+ if (i == 0 ) {
52
+ head = newNode ;
53
+ }
54
+
55
+ if (node == null ) {
56
+ node = newNode ;
57
+ } else {
58
+ node .next = newNode ;
59
+ node = newNode ;
60
+ }
61
+ }
62
+
63
+ return head ;
64
+ }
65
+
66
+ private static void printOutput (SinglyLinkedNode newHead ) {
67
+ System .out .print ("[" );
68
+ do {
69
+ System .out .print (newHead .val );
70
+ newHead = newHead .next ;
71
+ if (newHead != null ) {
72
+ System .out .print ("," );
73
+ }
74
+ } while (newHead != null );
75
+ System .out .println ("]" );
76
+ }
77
+
78
+ /**
79
+ * Given the head of a linked list, rotate the list to the right by k places.
80
+ * @param head
81
+ * @param k
82
+ * @return head of linked list
83
+ */
84
+ public SinglyLinkedNode rotateRight (SinglyLinkedNode head , int k ) {
85
+ if (k == 0 || head == null || head .next == null ) {
86
+ return head ;
87
+ }
88
+
89
+ // Finding the size of the list
90
+ int size = 1 ; // start with 1 because we're starting with head
91
+ SinglyLinkedNode currentEnd = head ;
92
+ while (currentEnd .next != null ) {
93
+ currentEnd = currentEnd .next ;
94
+ size ++;
95
+ }
96
+
97
+ // linking the end to the head
98
+ currentEnd .next = head ;
99
+
100
+ // determine the rotation based on k
101
+ int rotate = 0 ;
102
+ if (k < size ) {
103
+ rotate = k ;
104
+ } else {
105
+ rotate = k % size ;
106
+ }
107
+
108
+ // Find the index of the end node
109
+ int endNode = size - rotate ;
110
+
111
+ // set the newEnd to head and loop until you get to the new end node
112
+ SinglyLinkedNode newEnd = head ;
113
+ for (int i = 1 ; i < endNode ; i ++) {
114
+ newEnd = newEnd .next ;
115
+ }
116
+
117
+ // set the new head node and terminate the new end node
118
+ SinglyLinkedNode newHead = newEnd .next ;
119
+ newEnd .next = null ;
120
+ return newHead ;
121
+ }
122
+ }
0 commit comments