I met this problem of swapping consecutive two nodes from beginning to end of a linked list.(Eg. [1,2,3,4] to [2,1,4,3] or [1,2,3,4,5] to [2,1,4,3,5]) I found the pointer to pointer solution hard to understand. Can anyone help me on the following codes
ListNode* swapPairs(ListNode* head) {
ListNode **pp = &head, *a, *b;
while ((a = *pp) && (b = a->next)) {
a->next = b->next;
b->next = a;
*pp = b;
pp = &(a->next);
}
return head;
}
I can't understand the line *pp = b. why it only changes the 'head' to 'b' but keeps 'a' untouched? when debugging, 'a' and 'head' are the same before this line.