-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.c
81 lines (64 loc) · 1.39 KB
/
question.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
78
79
80
//reverse linked list in groups of sizes
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
void push(struct Node** head,int val){
struct Node *ptr=(struct Node *)malloc(sizeof(struct Node));
ptr->next = *head;
ptr->data=val;
*head=ptr;
}
void lltrav(struct Node *head){
struct Node * p=head;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
struct Node *reversal(struct Node *head){
struct Node *curr=head;
struct Node *prev=NULL;
struct Node *nextn=head->next;
while(curr->next!=NULL){
curr->next = prev;
prev=curr;
curr=nextn;
nextn=curr->next;
}
curr->next = prev;
head=curr;
return head;
}
struct Node *reverselengths(struct Node *head,int k){
struct Node *current=head;
struct Node *next=NULL;
struct Node *prev = NULL;
int count = 0;
while(current!=NULL && count < k){
next = current->next;
current->next = prev;
prev=current;
current=next;
count++;
}
if(next!=NULL){
head->next = reverselengths(next,k);
}
return prev;
};
int main(){
struct Node *head=NULL;
push(&head,5);
push(&head,4);
push(&head,3);
push(&head,2);
push(&head,1);
lltrav(head);
head=reverselengths(head,3);
lltrav(head);
return 0;
}