Leetcode#24 Swap Nodes in Pairs
2015-07-10 02:09
609 查看
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
Difficulty: Medium
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.
Difficulty: Medium
ListNode* swapPairs(ListNode* head) { ListNode* first; ListNode* second; ListNode* third; ListNode* temp = new ListNode(0);; ListNode* beg; if(!head || !head -> next) { return head; } first = head; beg = head->next; while(first &&first->next) { third = first->next->next; second = first->next; temp ->next = second; first->next = second->next; second->next = first; temp = first; first = first->next; } return beg; }
相关文章推荐
- Webpack 性能优化 (一)(使用别名做重定向)
- Node.js 应用性能优化的五个技巧
- [LintCode] Remove Node in Binary Search Tree
- [LeetCode][Java] Remove Nth Node From End of List
- LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解
- Node.js学习笔记 02 Implementing flow control
- Socket.IO 和 Node.js 入门(转)
- 【LeetCode】222. Count Complete Tree Nodes
- 【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes
- leetcode 019 —— Remove Nth Node From End of List
- nodejs对文件进行分页
- node.js+express,实现RESTful API
- Node.js 入门
- node.js通过edge访问.net动态链接库
- LeetCode——Count Complete Tree Nodes
- ubuntu / Ubuntu Kylin下安装和卸载 Nodepadqq
- Django:ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
- Populating Next Right Pointers in Each Node
- Innode表空间碎片优化
- Swap Nodes in Pairs