24. Swap Nodes in Pairs
2018-01-03 00:27
267 查看
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* swapPairs(ListNode* head) { ListNode *res = head, *next, *after; if (head && head->next) res = head->next; while (head) { next = head->next; if (next) { after = next->next; next->next = head; if (after && after->next) head->next = after->next; else head->next = after; head = after; } else { break; } } return res; } };
陷阱多,WA了几次才最终修正。感觉自己总要在debug中纠错,还需要提高姿势水平。
相关文章推荐
- LeetCode 24 Swap Nodes in Pairs (链表 递归&&非递归)
- [Leetcode] 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- [LeetCode]--24. Swap Nodes in Pairs
- LeetCode代码记录 24 Swap Nodes in Pairs
- [Leetcode 24, Medium] Swap Nodes in Pairs
- leecode 解题总结:24 Swap Nodes in Pairs
- [leetcode 24] Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- leetcode——24—— Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- LeetCode 24 - Swap Nodes in Pairs
- LeetCode(24) Swap Nodes in Pairs
- LeetCode24 Swap Nodes in Pairs 25. Reverse Nodes in k-Group详解
- LeetCode 24. Swap Nodes in Pairs
- 24. Swap Nodes in Pairs
- 【leetcode】【24】Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs