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* removeNode(Node* head,int idx){
  12. if(head == nullptr){
  13. return nullptr;
  14. };
  15. if(idx == 0){
  16. Node* nw = head->next;
  17. head->next = nullptr;
  18. return nw;
  19. }
  20. Node* curr = head;
  21. int cnt = 0;
  22. while(cnt < idx - 1 && curr != nullptr ){
  23. curr = curr->next;
  24. cnt++;
  25. }
  26.  
  27. if(curr == nullptr || curr->next == nullptr){
  28. return head;
  29. }
  30.  
  31. Node* temp = curr->next;
  32. curr->next = temp->next;
  33.  
  34. delete temp;
  35.  
  36. return head;
  37. }
  38. Node* LL(vector<int>&a){
  39. Node* head = new Node(a[0]);
  40. Node* curr = head;
  41.  
  42. for(int i = 1 ; i < a.size();i++){
  43. curr->next = new Node(a[i]);
  44. curr = curr->next;
  45. }
  46. return head;
  47. }
  48.  
  49. void print(Node* head){
  50. Node* temp = head;
  51.  
  52. while(temp != nullptr){
  53. cout<<temp->val<<endl;
  54. temp = temp->next;
  55. }
  56. }
  57. int main() {
  58. int n,i ; cin>>n>>i;
  59. vector<int>a(n);
  60.  
  61. for(int i = 0;i<n ;i++){
  62. cin>>a[i];
  63. }
  64.  
  65. Node* head = LL(a);
  66. Node* ans = removeNode(head,i);
  67.  
  68. print(ans);
  69. return 0;
  70. }
Success #stdin #stdout 0.01s 5324KB
stdin
5 2
1 2 3 4 5
stdout
1
2
4
5