fork download
  1. #include<iostream>
  2. using namespace std;
  3. class node{
  4. public:
  5. long long int data;
  6. node *next;
  7. node(long long int d){
  8. data = d;
  9. next = NULL;
  10. }
  11. };
  12.  
  13. void insertAtTail(node *&head, long long int data){
  14. if(head==NULL){
  15. head= new node(data);
  16. return;
  17. }
  18. node *tail = head;
  19. while(tail->next!=NULL){
  20. tail=tail->next;
  21. }
  22. tail->next=new node(data);
  23. return;
  24. }
  25.  
  26.  
  27. void ll_k_append(node *&head, long long int k, long long int n){
  28. node *temp = head;
  29. node *temp2 = head;
  30. if(k==n){
  31. return;
  32. }
  33. else if(k>n){
  34. k=k%n;
  35. }
  36. else if(k<n){
  37. for(int i=0;i<(n-k);i++){
  38. temp = temp->next;
  39. }
  40. temp2=temp;
  41. temp = temp2->next;
  42. temp2->next = NULL;
  43.  
  44. while(temp->next!=NULL){
  45. temp = temp->next;
  46. }
  47. temp->next = head;
  48. head = temp;
  49. }
  50.  
  51. }
  52.  
  53. void print(node *head){
  54. while(head!=NULL){
  55. cout<<head->data<<" ";
  56. head = head->next;
  57. }
  58. }
  59. int main(){
  60. node *head = NULL;
  61. long long int n,k,data;
  62. cin>>n;
  63. for(int i=0;i<n;i++){
  64. cin>>data;
  65. insertAtTail(head,data);
  66. }
  67.  
  68. cin>>k;
  69. ll_k_append(head,k,n);
  70. print(head);
  71.  
  72. return 0;
  73. }
  74.  
Time limit exceeded #stdin #stdout 5s 5688KB
stdin
Standard input is empty
stdout
Standard output is empty