-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_64.cpp
140 lines (106 loc) · 2.44 KB
/
Day_64.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
DAY 64 : Reverse a Linked List in groups of given size.
https://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/
QUESTION : Given a linked list of size N. The task is to reverse every k
nodes (where k is an input to the function) in the linked list.
Example:
Input:
LinkedList: 1->2->2->4->5->6->7->8
K = 4
Output: 4 2 2 1 8 7 6 5
Explanation:
The first 4 elements 1,2,2,4 are reversed first
and then the next 4 elements 5,6,7,8. Hence, the
resultant linked list is 4->2->2->1->8->7->6->5.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 10^4
1 <= k <= N
*/
# include <iostream>
using namespace std;
class LinkedList
{
struct Node
{
int data;
Node *next = NULL;
} *head;
public:
LinkedList()
{
head = NULL;
}
void insert(int val);
void display();
void reverse(int k);
};
void LinkedList::insert(int val)
{
Node *newnode = new Node;
newnode->data = val;
if (head == NULL)
{
head = newnode;
}
else
{
Node *current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = newnode;
}
}
void LinkedList::display()
{
cout<<"Displaying: ";
Node *current = head;
while (current->next != NULL)
{
cout<<current->data<<" ";
current = current->next;
}
cout<<current->data<<endl;
}
void LinkedList::reverse(int k)
{
Node *current = head;
Node *prev = NULL;
Node *next = NULL;
int count = 0;
if (head->next == NULL || k==1) {
return;
}
while (count<k) {
count++;
next = current->next;
if (count == k) {
Node* save = current->next;
head -> next = save;
}
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
int main()
{
LinkedList ll_obj;
int N;
cout<<"Enter N: ";
cin>>N;
cout<<"Enter elements to insert: ";
for (int i = 0; i < N; i++)
{
int each;
cin>>each;
ll_obj.insert(each);
}
ll_obj.reverse(1);
ll_obj.display();
return 0;
}