1
class Solution {
2
public:
3
ListNode *swapPairs(ListNode *head) {
4
// if head is NULL OR just having a single node, then no need to change
5
// anything
6
if (head == NULL || head->next == NULL) {
7
return head;
8
}
9

10
ListNode *temp; // temporary pointer to store head -> next
11
temp = head->next; // give temp what he want
12

13
head->next = swapPairs(head->next->next); // changing links
14
temp->next = head; // put temp -> next to head
15

16
return temp; // now after changing links, temp act as our head
17
}
18
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0