14
14
* </p>
15
15
* <p>
16
16
* The implementation contains:
17
- * - {@code length(Node head)}: A method to calculate the length of the linked list.
18
- * - {@code reverse(Node head, int count, int k)}: A helper method that reverses the nodes
17
+ * - {@code length(SinglyLinkedListNode head)}: A method to calculate the length of the linked list.
18
+ * - {@code reverse(SinglyLinkedListNode head, int count, int k)}: A helper method that reverses the nodes
19
19
* in the linked list in groups of k.
20
- * - {@code reverseKGroup(Node head, int k)}: The main method that initiates the reversal
20
+ * - {@code reverseKGroup(SinglyLinkedListNode head, int k)}: The main method that initiates the reversal
21
21
* process by calling the reverse method.
22
22
* </p>
23
23
* <p>
@@ -38,8 +38,8 @@ public class ReverseKGroup {
38
38
* @param head The head node of the linked list.
39
39
* @return The total number of nodes in the linked list.
40
40
*/
41
- public int length (Node head ) {
42
- Node curr = head ;
41
+ public int length (SinglyLinkedListNode head ) {
42
+ SinglyLinkedListNode curr = head ;
43
43
int count = 0 ;
44
44
while (curr != null ) {
45
45
curr = curr .next ;
@@ -56,14 +56,14 @@ public int length(Node head) {
56
56
* @param k The size of the group to reverse.
57
57
* @return The new head of the reversed linked list segment.
58
58
*/
59
- public Node reverse (Node head , int count , int k ) {
59
+ public SinglyLinkedListNode reverse (SinglyLinkedListNode head , int count , int k ) {
60
60
if (count < k ) {
61
61
return head ;
62
62
}
63
- Node prev = null ;
63
+ SinglyLinkedListNode prev = null ;
64
64
int count1 = 0 ;
65
- Node curr = head ;
66
- Node next = null ;
65
+ SinglyLinkedListNode curr = head ;
66
+ SinglyLinkedListNode next = null ;
67
67
while (curr != null && count1 < k ) {
68
68
next = curr .next ;
69
69
curr .next = prev ;
@@ -85,7 +85,7 @@ public Node reverse(Node head, int count, int k) {
85
85
* @param k The size of the group to reverse.
86
86
* @return The head of the modified linked list after reversal.
87
87
*/
88
- public Node reverseKGroup (Node head , int k ) {
88
+ public SinglyLinkedListNode reverseKGroup (SinglyLinkedListNode head , int k ) {
89
89
int count = length (head );
90
90
return reverse (head , count , k );
91
91
}
0 commit comments