week10-leetcode #24-Swap-Nodes-in-Pairs
来源:互联网 发布:django网站开发源码 编辑:程序博客网 时间:2024/06/06 00:54
week10-leetcode #24-Swap-Nodes-in-Pairs
链接:https://leetcode.com/problems/swap-nodes-in-pairs/description/
Question
Given a linked list, swap every two adjacent nodes and return its head.
Example
Given 1->2->3->4, you should return the list as 2->1->4->3.
Solution
time complecity:
space complecity:
class Solution { public: ListNode* swapPairs(ListNode* head) { int pre_val = 0; int post_val = 0; ListNode* p = head; // 不断往前交换数据 while (p != NULL && p->next != NULL) { pre_val = p->val; post_val = p->next->val; p->val = post_val; p->next->val = pre_val; p = p->next->next; } return head; }};
思路:本题目只对偶数对进行处理,也就是(0,1)、(2,3)、(4,5)等进行配对(下标从0开始),最后的奇数即可以直接忽略不处理。
阅读全文
0 0
- week10-leetcode #24-Swap-Nodes-in-Pairs
- [LeetCode-Algorithms-24] "Swap Nodes in Pairs" (2017.11.7-WEEK10)
- 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
- 非递归遍历后序二叉树
- unplug / plug PDBS
- HTML常见基础元素整理
- 一致性哈希算法
- C语言实现的2048小游戏
- week10-leetcode #24-Swap-Nodes-in-Pairs
- 二叉树线索化
- 刚学链表,有点搞不懂,求教
- 矩阵键盘
- 2、注解@PathVariable用法——映射URL绑定的占位符
- 42. Trapping Rain Water
- Linux下访问Tomcat只能通过localhost,不能通过IP,怎么解决
- UKF MATLAB
- 非递归遍历二叉树