Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

zhenialeks May 20th, 2020 995 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5.     int n;
  6.     struct node *next;
  7. } Node;
  8.  
  9. Node *init_node(int n){
  10.     Node *tmp = (Node *) malloc(sizeof(Node));
  11.     tmp->n = n;
  12.     tmp->next = NULL;
  13.     return tmp;
  14. }
  15.  
  16.  
  17. Node *remove_nth_node(Node *cur_ptr, int n, int len){
  18.     Node *tmp = cur_ptr;
  19.     Node *node_to_free;
  20.  
  21.     if (n == 1)
  22.         for (int i = 0; i < len-1; i++)
  23.             tmp = tmp->next;
  24.     else
  25.         for (int i = 0; i < n-2; i++)
  26.             tmp = tmp->next;
  27.  
  28.  
  29.     node_to_free = tmp->next;
  30.     tmp->next = tmp->next->next;
  31.     free(node_to_free);
  32.  
  33.     return tmp->next;
  34. }
  35.  
  36. int main(){
  37.  
  38.     int n, k;
  39.     Node *head, *tmp_ptr;
  40.     scanf("%d %d", &n, &k);
  41.  
  42.     head = init_node(1);
  43.     tmp_ptr = head;
  44.  
  45.     for (int i = 1; i < n; i++){
  46.         tmp_ptr->next = init_node(i+1);
  47.         tmp_ptr = tmp_ptr->next;
  48.     }
  49.     tmp_ptr->next = head;
  50.  
  51.     tmp_ptr = head;
  52.  
  53.     if (tmp_ptr == NULL) exit(EXIT_FAILURE);
  54.  
  55.     while (tmp_ptr ->next != tmp_ptr){
  56.         tmp_ptr = remove_nth_node(tmp_ptr, k, n);
  57.         n--;
  58.     }
  59.     printf("%d",tmp_ptr->n);
  60.  
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top