This problem looks busy at first, but the accepted solution is built around one steady invariant. For 24. Swap Nodes in Pairs, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are swap, swapPairs.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Swap Nodes in Pairs.
02//Memory Usage: 7.9 MB, less than 8.24% of C++ online submissions for Swap Nodes in Pairs.
03/**
04 * Definition for singly-linked list.
05 * struct ListNode {
06 * int val;
07 * ListNode *next;
08 * ListNode() : val(0), next(nullptr) {}
09 * ListNode(int x) : val(x), next(nullptr) {}
10 * ListNode(int x, ListNode *next) : val(x), next(next) {}
11 * };
12 */
13class Solution {
14public:
15 ListNode* swap(ListNode* node){
16 //length 0 or 1
17 if(!node || !node->next)
18 return node;
19
20 ListNode* next = node->next;
21 node->next = next->next;
22 next->next = node;
23
24 return next;
25 }
26 ListNode* swapPairs(ListNode* head) {
27 ListNode* dummy = new ListNode(0);
28
29 dummy->next = head;
30
31 ListNode* cur = dummy;
32
33 while(cur){
34 ListNode* next = swap(cur->next);
35 cur->next = next;
36
37 cur = cur->next;
38 if(cur)cur = cur->next;
39 }
40
41 return dummy->next;
42 }
43};
44
45//recursion
46//https://leetcode.com/problems/swap-nodes-in-pairs/discuss/11030/My-accepted-java-code.-used-recursion.
47//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Swap Nodes in Pairs.
48//Memory Usage: 7.9 MB, less than 9.71% of C++ online submissions for Swap Nodes in Pairs.
49class Solution {
50public:
51 ListNode* swapPairs(ListNode* head) {
52 if(!head || !head->next)
53 return head;
54
55 ListNode* next = head->next;
56 head->next = swapPairs(head->next->next);
57 next->next = head;
58
59 return next;
60 }
61};
Cost