-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp167.cpp
81 lines (64 loc) · 1.08 KB
/
p167.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node* next;
};
Node *kAltReverse(Node *head, int k)
{
Node* current = head;
Node* next;
Node* prev = NULL;
int count = 0;
while (current != NULL && count < k)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
count++;
}
if(head != NULL)
head->next = current;
count = 0;
while(count < k-1 && current != NULL )
{
current = current->next;
count++;
}
if(current != NULL)
current->next = kAltReverse(current->next, k);
return prev;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node *node)
{
int count = 0;
while(node != NULL)
{
cout<<node->data<<" ";
node = node->next;
count++;
}
}
int main(void)
{
Node* head = NULL;
int i;
for(i = 20; i > 0; i--)
push(&head, i);
cout<<"Given linked list \n";
printList(head);
head = kAltReverse(head, 3);
cout<<"\n Modified Linked list \n";
printList(head);
return(0);
}