ListNode *swapPairs(ListNode *head) {
ListNode *p, *q;
p = head;
while (p>next != NULL){
q = p>next>next;
p>next>next = p;
if (p == head) head = p>next;
if (q == NULL){
p>next = NULL; break;
}
if (q>next == NULL) p>next = q;
else p>next = q>next;
p = q;
}
return head;
}
Works under IDE, unacceptable here with runtime error.What's wrong?

Solved.
class Solution { public: ListNode *swapPairs(ListNode *head) { if(head == NULL) return head; ListNode *p, *q; p = head; while (p>next != NULL){ q = p>next>next; p>next>next = p; if (p == head) head = p>next; if (q == NULL){ p>next = NULL; break; } if (q>next == NULL) p>next = q; else p>next = q>next; p = q; } return head; } };