-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverse_DLL_in_groups_of_given_size.cpp
91 lines (73 loc) · 1.61 KB
/
Reverse_DLL_in_groups_of_given_size.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
/*
Problem Statement:
-----------------
Given a doubly linked list containing n nodes. The problem is to reverse every group of k nodes in the list.
Examples:
--------
Input : DLL : 10 <-> 8 <-> 4 <-> 2
Output: DLL : 8 <-> 10 <-> 2 <-> 4
*/
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next, *prev;
};
Node *getNode(int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = new_node->prev = NULL;
return new_node;
}
void push(Node **head_ref, Node *new_node)
{
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void printList(Node *head)
{
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
}
Node *revListInGroupOfGivenSize(Node *head, int k)
{
int counter = 0;
Node *current = head;
Node *prev = NULL;
Node *next = NULL;
while(counter != k and current != NULL)
{
next = current->next;
current->prev = next;
current->next = prev;
prev = current;
current = next;
counter++;
}
if(next != NULL)
head->next = revListInGroupOfGivenSize(next, k);
return prev;
}
int main()
{
Node *head = NULL;
push(&head, getNode(2));
push(&head, getNode(4));
push(&head, getNode(8));
push(&head, getNode(10));
int k = 2;
cout << "Original list is : ";
printList(head);
head = revListInGroupOfGivenSize(head, k);
cout << "\nModified list is : ";
printList(head);
return 0;
}