Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct Node {
- int data;
- struct Node* next;
- };
- int cnt = 0;
- struct Node *reverse (struct Node *head, int k, int SZ) {
- if (!head)
- return NULL;
- struct Node *current = head;
- struct Node *next = NULL;
- struct Node *prev = NULL;
- int count = 0;
- if (abs(cnt - SZ) < k)
- return head;
- while (current != NULL && count < k) {
- cnt++;
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- count++;
- }
- if (next != NULL)
- head->next = reverse(next, k, SZ);
- return prev;
- }
- void push(struct Node** head_ref, int new_data) {
- struct Node *new_node = (struct Node *) malloc(sizeof(struct Node));
- new_node->data = new_data;
- new_node->next = (*head_ref);
- (*head_ref) = new_node;
- }
- const int MAX_SZ = 10009;
- int arr[MAX_SZ];
- int main(void) {
- struct Node *head = NULL;
- int nodes, sz;
- scanf("%d%d", &nodes, &sz);
- int x;
- for (int i = 0; i < nodes; ++i) {
- scanf("%d", &x);
- arr[i] = x;
- }
- for (int i = nodes - 1; i >= 0; --i) {
- push(&head, arr[i]);
- }
- head = reverse(head, sz, nodes);
- while (head != NULL) {
- printf("%d\n", head->data);
- head = head->next;
- }
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement