I have submitted code which is giving wrong answer but
for custom inputs it is correct.
Sample input is working but wrong answer
#include
using namespace std;
class Node{
public:
int data;
Node next;
Node(int data){
this -> data = data;
this -> next = NULL;
}
};
Node takeinput(int n) {
int data;
cin >> data;
Node* head = NULL, *tail = NULL;
while(n > 0){
Node *newNode = new Node(data);
if(head == NULL) {
head = newNode;
tail = newNode;
}
else{
tail -> next = newNode;
tail = newNode;
}
cin >> data;
nā;
}
return head;
}
void print(Node *head) {
Node *temp = head;
while(temp != NULL) {
cout << temp -> data <<"-> ";
temp = temp -> next;
}
cout<<endl;
}
Node* swapAlternateNodes(Node* head)
{
if(head == NULL || head->next == NULL)
{
return head;
}
Node* smallans = swapAlternateNodes(head->next->next);
Node* n1 = head;
Node* n2 = head->next;
n2->next = n1;
n1->next = smallans;
return n2;
}
int main() {
int n;
cin>>n;
//int k;
//cin>>k;
Node head = takeinput(n);
print(head);
Node newhead = swapAlternateNodes(head);
print(newhead);
return 0;
}
Hey @updesh13
Code is correct error is that while printing you missed a space before arrow
replace line 35 by
cout << temp -> data <<" -> ";