leetcode swap nodes in pairs
来源:互联网 发布:xp 在端口23连接失败 编辑:程序博客网 时间:2024/04/28 04:49
很简单的一道链表题,还是错了orz
/** * 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) { if(head == NULL){ return NULL; } if(head->next == NULL){ return head; } ListNode dummy(-1); dummy.next = head; ListNode* prev = &dummy, *tmp = head; while(tmp!= NULL && tmp->next != NULL){ ListNode* pprev = tmp; ListNode* nnext = tmp->next; tmp->next = nnext->next; nnext->next = tmp; prev->next = nnext; prev = tmp; tmp = tmp->next; } return dummy.next; }};
错误原因:没有对链表长度为奇数or偶数的情况分开考虑。。考虑不够周全。。。
0 0
- LeetCode:Swap Nodes in Pairs
- LeetCode: Swap Nodes in Pairs
- [Leetcode] Swap Nodes in Pairs
- LeetCode : Swap Nodes in Pairs
- [LeetCode] Swap Nodes in Pairs
- Leetcode: Swap nodes in pairs
- [LeetCode]Swap Nodes in Pairs
- [LeetCode]Swap Nodes in Pairs
- LeetCode-Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- LeetCode | Swap Nodes in Pairs
- 【leetcode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- LeetCode - Swap Nodes in Pairs
- 【LeetCode】Swap Nodes in Pairs
- Leetcode: Swap Nodes in Pairs
- Leetcode Swap Nodes in Pairs
- LeetCode Swap Nodes in Pairs
- 线性表链式存储(单链表)及其15种操作的实现
- 利用javascript来获取当前时间戳 小技巧new Data()
- Java 中使用内存映射文件需要考虑的 10 个问题
- 线性表链式存储(静态链表)及其12种操作的实现
- 链表概述
- leetcode swap nodes in pairs
- C & Prime
- Node.js静态资源类文件请求处理
- 线性表顺序存储(数组)及其15种操作的实现
- 单链表插入删除元素时间复杂度探究
- c/c++学习工具 CodeBlocksIDE
- Swift XXXLiteralConvertible 协议的使用
- .post简化.ajax方式提交form表单
- android系统深入研究