What the problem in this code?


#1

Comment bodyListNode* Solution::swapPairs(ListNode* A) {
if(A==NULL||A->next==NULL)
{
return A;
}

ListNode* p=A;ListNode* q=A->next;
ListNode* head=q;ListNode* temp=head->next;
head->next=p;
while(p&&q&&p->next!=NULL)
{
p=p->next->next;
q=q->next->next;
temp->next=q;
temp->next->next =p;
temp=temp->next->next;

}if(p!=NULL)
{
temp->next=p;
}return head;
} goes here.


#2

bro i read ur code nd i think u have used way too many pointers nd thats why it got so complicated to even dry run code ur code.its a very simple question nd can be sloved easily using one pointer only.plz have a look at this code:-

ListNode* Solution::swapPairs(ListNode* A) {
ListNode* temp=A;
while(temp!=NULL&&temp->next!=NULL)
{
int first=temp->val;
int second=temp->next->val;
temp->val=second;
temp->next->val=first;
temp=temp->next->next;
}
return A;

thanks…enjoy