leetcode 24. Swap Nodes in Pairs
来源:互联网 发布:greenvpn网络加速器 编辑:程序博客网 时间:2024/05/29 03:02
相关问题
Discription
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.
思路
模拟题
时间复杂度:
空间复杂度:
代码
/** * 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 *pCur, *pNode2, *pNode3, *pHead; if (!head || !head->next) return head; pHead = head->next; pCur = head; // 1 while (pCur && pCur->next) { pNode2 = pCur->next; // 2 pNode3 = pNode2->next; // 3 if (!pNode3 || !pNode3->next) pCur->next = pNode3; else pCur->next = pNode3->next; pNode2->next = pCur; pCur = pNode3; } return pHead; }};
阅读全文
0 0
- [LeetCode]24.Swap Nodes in Pairs
- 24. Swap Nodes in Pairs Leetcode Python
- 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
- 24. Swap Nodes in Pairs LeetCode
- 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
- LeetCode 24. Swap Nodes in Pairs
- MySQL笔记1
- python3中的map函数详解
- java使用正则替换网页连接中的href=“....”的内容
- Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法
- 如何使用 Hexo && 部署至GitHub Pages [tags] [翻译]
- leetcode 24. Swap Nodes in Pairs
- HDU 1559 最大子矩阵
- chrome浏览器 配置开机启动全屏
- hadoop2.8.2官方文档之分布式集群
- Redis 有序集合(sorted set)
- Java学习笔记(7)
- 15.4.1 处理流的用法
- 排序算法@c++描述-插入排序
- EPUB是什么格式?PDF如何转换成EPUB格式?