fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct Node{
  5. int val;
  6. Node* next;
  7.  
  8. Node(int val):val(val),next(nullptr){};
  9. };
  10.  
  11. Node* addNode(Node* head,int u,int idx){
  12. if(head == nullptr){
  13. head->val = u;
  14. return head;
  15. };
  16. if(head->next == nullptr){
  17. head->next->val = u;
  18. }
  19. Node* curr = head;
  20.  
  21. int cnt = 0;
  22. while(curr!=nullptr && cnt<=idx ){
  23. curr = curr->next;
  24. cnt++;
  25. }
  26. curr->val = u;
  27. return head;
  28. }
  29. Node* LL(vector<int>&a){
  30. Node* head = new Node(a[0]);
  31. Node* curr = head;
  32.  
  33. for(int i = 0 ; i < a.size();i++){
  34. curr->next = new Node(a[i]);
  35. curr = curr->next;
  36. }
  37. return head;
  38. }
  39.  
  40. void print(Node* head){
  41. Node* temp = head;
  42.  
  43. while(temp != nullptr){
  44. cout<<temp->val<<endl;
  45. temp = temp->next;
  46. }
  47. }
  48. int main() {
  49. int n,k,i ; cin>>n>>k>>i;
  50. vector<int>a(n);
  51.  
  52. for(int i = 0;i<n ;i++){
  53. cin>>a[i];
  54. }
  55.  
  56. Node* head = LL(a);
  57. Node* ans = addNode(head,k,i);
  58.  
  59. print(ans);
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5308KB
stdin
5 2 1
1 2 3 5 6
stdout
1
1
2
3
5
6