24. Swap Nodes in Pairs leetcode Python 2016 new Season
2016-01-10 12:07
645 查看
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.
Show Company Tags
Show Tags
Show Similar Problems
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.
Show Company Tags
Show Tags
Show Similar Problems
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def swapPairs(self, head): """ :type head: ListNode :rtype: ListNode """ if not head or not head.next: return head dummyHead = pNode = ListNode(None) pNode.next = head while pNode.next and pNode.next.next: tmp = pNode.next.next pNode.next.next = tmp.next tmp.next = pNode.next pNode.next = tmp pNode = pNode.next.next return dummyHead.next
相关文章推荐
- (2-3)DateNode+小结HDFS
- nodejs摘要
- [LeetCode] 116. Populating Next Right Pointers in Each Node 解决思路
- gulp-nodemon 和 gulp-livereload 配置
- nodejs 中模块使用的介绍
- Node.js静态文件服务器改进版
- Node.js静态文件服务器改进版
- 基于Node.js的强大爬虫 能直接发布抓取的文章哦
- nodejs小项目之用户管理
- Node.js静态文件服务器
- NodeJS in Visual Studio
- [Pomelo]安装与部署Helloworld
- 几种IO模型的比较
- nodejs+express 4.x笔记
- leetcode之Reverse Nodes in k-Group
- leetcode之Swap Nodes in Pairs
- 20160109 Node.js 常用开发工具的使用
- nodejs 负载均衡
- Nodejs建站3-Blog-摇一摇功能-boostrap-express-mvc-mongod-jade
- Hadoop2.6 datanode配置在线更新