-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkReverse.c
77 lines (64 loc) · 2.23 KB
/
chunkReverse.c
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
void addNode(struct node **headaddress, int value){
struct node *temp = malloc(sizeof(struct node));
temp->data = value;
temp->next = *headaddress;
*headaddress = temp;
}
void ReverseKthNodes(struct node **headaddress, int key){
struct node *temp = *headaddress;
int c = 1 ; // count variable
while(c < key) { // count keeps track of the position of where to divide the original linked list.
temp = temp->next;
c++;
}
struct node *breakPoint = temp->next ; // temp->next is the address of the node where the new linked list starts.
temp->next = NULL; // Make the address contained in the node before the key to be null now.
struct node *prev = NULL; // Initially both prev and next have NULL as the addresses.
struct node *current = *headaddress;
struct node *next;
while (current != NULL) // Reverse the list till Kth node.
{ // At the end head has to be NULL coz it would be the last node which contains some data but the address is NULL.
next = current->next; // Reverse technique.
current->next = prev;
prev = current;
current = next;
}
*headaddress = prev; // At the end of above loop head points to the next node. Since the next node would not exist at the end of the list head has to have addre. of prev which is the last address of the list.
current = *headaddress;
while(current->next != NULL){
current = current->next;
current->next = breakPoint;
}
}
void printNodes(struct node *headaddress)
{
printf("Elements are : \n");
while (headaddress != NULL)
{
printf("%d\n", headaddress->data);
headaddress = headaddress->next;
}
}
int main(){
struct node *head = NULL;
addNode(&head, 5);
addNode(&head, 10);
addNode(&head, 15);
addNode(&head, 20);
addNode(&head, 25);
addNode(&head, 30);
addNode(&head, 35);
addNode(&head, 40);
printf("The elements of the nodes are: \n");
printNodes(head);
printf("After modification, elements of the nodes are: \n");
ReverseKthNodes(&head, 15);
printNodes(head);
return 0;
}