【Leetcode】Swap Nodes in Pairs
来源:互联网 发布:java里调用window.open 编辑:程序博客网 时间:2024/05/04 23:27
问题
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.
代码
class Solution {public: ListNode *swapPairs(ListNode *head) { if (!head) {return NULL;}if (head->next == NULL) {return head;}ListNode *returnNode = NULL;ListNode *lastNode = NULL;while (head) {if (head->next) {if (!returnNode) {returnNode = head->next;head->next = returnNode->next;returnNode->next = head;lastNode = head;}else{lastNode->next = head->next;head->next = head->next->next;lastNode->next->next = head;lastNode = head;}}else{break;}head=head->next;}return returnNode; }};
分析
链表,没有什么难度,一次性通过~~
总结
n/a
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
- 学习Golang语言(4):类型--字符串
- 可爱的Ubuntu
- leetcode:Rotate Image
- 移动端前端开发概述
- 基础数据库的创建知识---简记
- 【Leetcode】Swap Nodes in Pairs
- Oracle 10g SQL分页查询语句和效率分析
- 程序内部打开某个APP在AppStore的购买页面
- 【Leetcode】Reverse Nodes in k-Group
- javascript 动态判断html元素
- DOS的系统基本命令---简记
- 请用美丽欢呼-------Day38
- C++中空类非空
- JUnit4 中@AfterClass @BeforeClass @after @before的区别对比