【LeetCode】Swap Nodes in Pairs
来源:互联网 发布:薛之谦淘宝店铺链接 编辑:程序博客网 时间:2024/05/22 14:46
题目描述:
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4
, you should return the list as 2->1->4->3
.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
题目要求不能只换val,需要将整个节点交换。用两个变量储存下就好了。代码:
class Solution {public:ListNode *swapPairs(ListNode *head) {ListNode *res = new ListNode(0);ListNode *node = res;while (head){ListNode *l1(NULL);ListNode *l2(NULL);if (head){l1 = head;head = head->next;}if (head){l2 = head;head = head->next;}if (l2){node->next = l2;node = node->next;}if (l1){node->next = l1;node = node->next;}}node->next=NULL;return res->next;}};
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
- spring3.0事务的多数据源的annotation-driven用法
- Spring @Transactional 如何开启事务
- TextView/EditText 使用注意地方
- 图像拼接中的RANSAC算法
- Android配置QQ邮箱问题
- 【LeetCode】Swap Nodes in Pairs
- Linux设置环境变量小结:设置永久变量&临时变量 全局变量&局部变量
- Armstrong数问题
- U盘明明有空间 但复制大文件显示磁盘已满
- 酷派7296刷机包 高仿IOS美化 细节精仿
- android自定义progressbar图片大小自适应
- github也疯狂
- 不在该文件中的整数1GB
- ORACLE如何设置多路复用control files