[Leetcode]Swap Nodes in Pairs
来源:互联网 发布:java循环#拼接字符串 编辑:程序博客网 时间:2024/05/18 02: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.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: /*algorithm 1)three pointer, tail,shead,stail 2)swap shead,stail 3)move tail to shead time O(n) space O(1) */ ListNode* swapPairs(ListNode* head) { ListNode dummy(0),*tail = &dummy; ListNode*shead,*stail; dummy.next = head; while(tail && tail->next&& tail->next->next){ shead= tail->next; stail = shead->next; shead->next = stail->next; stail->next = shead; tail->next = stail; tail = shead; } return dummy.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
- VirtualBox在Linux中无法打开虚拟机解决办法
- 电商52个专业名词解释大汇总
- 路由器设置成交换机
- java中的tryCatch之
- SpriteBuilder中子节点的相对位置(%百分比定位)
- [Leetcode]Swap Nodes in Pairs
- mysql (一.建表)
- 我的博客说明
- 《当幸福来敲门》观后感
- 《赢在中国-蓝天碧水间》观后感
- Error: cannot open C:\Users\(用户名)\AppData\Local\Temp\internaljobs.obj.5628.140.jom for write
- JavaScript表达式
- Rotate List
- mysql主从复制原理