leetcode 24 Swap Nodes in Pairs
来源:互联网 发布:知乎 朱炫 西游记 编辑:程序博客网 时间:2024/06/16 18:38
/** * 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) return nullptr; ListNode headd(-1); headd.next = head; ListNode *p,*n1,*n2; p = &headd; while(p && p->next && p->next->next) { n1 = p->next; n2 = p->next->next; p->next = n2; n1->next = n2->next; n2->next = n1; p = p->next->next; } return headd.next; }};这个题是为了交换相邻两个节点的数值,最后返回头节点,但是开始读题的时候在想如果是奇数的节点数怎么办。。然后发现如果是奇数最后一个节点就不用管了。在编写的时候while循环里面写成了while(p->next->next&&p->next&&p)疯狂报错。因为有种情况p已经是空了,所以p->next就一定会报错,把顺序放正就好了。
0 0
- LeetCode(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
- leetcode 24 Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- Leetcode 24 Swap Nodes in Pairs
- Swap Nodes in Pairs - LeetCode 24
- LeetCode 24 - Swap Nodes in Pairs
- leetcode 24 -- Swap Nodes in Pairs
- LeetCode 24:Swap Nodes in Pairs
- leetcode-24Swap 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
- [Leetcode 24, Medium] Swap Nodes in Pairs
- Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException这个错误信息
- 实验2
- 2D激光SLAM算法比较+cartographer
- 思岚科技亮相2017电子博览会 倍受瞩目
- dubbo 服务提供者 心跳机制
- leetcode 24 Swap Nodes in Pairs
- J.U.C--线程池ThreadPoolExecutor
- 请你,理性爱国
- Android开发各类常见错误解决方案
- RocketMQ与Kafka对比
- Android Studio中修改包名和应用applicationid
- 图片加载利器之Picasso(五)查漏补缺
- java实现快速排序
- sqlserver索引优化