fork download
  1. #include<iostream>
  2. using namespace std;
  3. class node{
  4. public:
  5. int data;
  6. node*next;
  7. node(int d){
  8. data = d;
  9. next = NULL;
  10. }
  11. };
  12. void insertion(node*&head, int data){
  13. if(head == NULL){
  14. node*n = new node(data);
  15. head = n;
  16. return;
  17. }
  18. node *temp = head;
  19. while(temp->next!=NULL){
  20. temp = temp->next;
  21. }
  22. temp->next = new node(data);
  23. return;
  24. }
  25. void fun(node* start)
  26. {
  27. if(start == NULL)
  28. return;
  29. cout<<start->data<<" ";
  30. if(start->next != NULL )
  31. fun(start->next->next);
  32. cout<<start->data<<" ";
  33. }
  34. void print(node*head,int n){
  35. for(int i=0;i<n;i++){
  36. cout<<head->data<<" ";
  37. head = head->next;
  38. }
  39. }
  40.  
  41. int main() {
  42. int n,k,data;
  43. node*head = NULL;
  44. cin>>n>>k;
  45. for(int i=0;i<n;i++){
  46. cin>>data;
  47. insertion(head,data);
  48. }
  49. // head = kth_reverse(head,k);
  50. // print(head,n);
  51. fun(head);
  52. return 0;
  53. }
Success #stdin #stdout 0s 4384KB
stdin
1 2 3 4 5 6 
stdout
3 3