forked from mrigaankzoro/Hacktoberfest24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_k_sizedGrpsInLL.cpp
97 lines (80 loc) · 2.3 KB
/
reverse_k_sizedGrpsInLL.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == nullptr || head->next == nullptr) {
return head;
}
// Count the number of nodes in the list
int cnt = 0;
ListNode* temp = head;
while (temp != nullptr) {
temp = temp->next;
cnt++;
}
// If the number of nodes is less than k, return the head as it is
if (cnt < k) {
return head;
}
// Reverse the first k nodes
ListNode* prev = nullptr;
ListNode* current = head;
ListNode* forward = nullptr;
int c = 0;
while (current != nullptr && c < k) {
forward = current->next;
current->next = prev;
prev = current;
current = forward;
c++;
}
// Recursively call the function for the next group
if (forward != nullptr) {
head->next = reverseKGroup(forward, k);
}
// prev is the new head of the reversed group
return prev;
}
};
// Helper function to create a linked list from an array
ListNode* createLinkedList(int arr[], int size) {
if (size == 0) return nullptr;
ListNode* head = new ListNode(arr[0]);
ListNode* current = head;
for (int i = 1; i < size; i++) {
current->next = new ListNode(arr[i]);
current = current->next;
}
return head;
}
// Helper function to print the linked list from a given node
void printLinkedList(ListNode* node) {
while (node != nullptr) {
cout << node->val << " ";
node = node->next;
}
cout << endl;
}
int main() {
// Sample input
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
// Create the linked list
ListNode* head = createLinkedList(arr, size);
// Create an instance of the Solution class
Solution solution;
// Define the value of k
int k = 2;
// Reverse the nodes in k-groups
ListNode* newHead = solution.reverseKGroup(head, k);
// Print the new linked list
printLinkedList(newHead);
return 0;
}