[LeetCode]题解(python):024-Swap Nodes in Pairs
2015-10-12 21:53
681 查看
[b]题目来源:[/b]
https://leetcode.com/problems/swap-nodes-in-pairs/
[b]题意分析:[/b]
给定一个链表,每两个相邻节点就行交换。比如1->2->3->4,得到2->1->4->3。要求不能改变节点的值,不能新建链表。
[b]题目思路:[/b]
这题是考链表的操作。首先建立一个头节点,将头节点指向第二个节点,然后再指向第一个节点,最后指向第三个节点,然后指针跳到第二个节点重复。
[b]代码(python):[/b]
View Code
转载请注明出处:http://www.cnblogs.com/chruny/p/4872950.html
https://leetcode.com/problems/swap-nodes-in-pairs/
[b]题意分析:[/b]
给定一个链表,每两个相邻节点就行交换。比如1->2->3->4,得到2->1->4->3。要求不能改变节点的值,不能新建链表。
[b]题目思路:[/b]
这题是考链表的操作。首先建立一个头节点,将头节点指向第二个节点,然后再指向第一个节点,最后指向第三个节点,然后指针跳到第二个节点重复。
[b]代码(python):[/b]
# 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 head == None: return None ans = ListNode(0) ans.next = head tmp = ans while tmp.next and tmp.next.next: t = tmp.next.next tmp.next.next = t.next t.next = tmp.next tmp.next = t tmp = tmp.next.next return ans.next
View Code
转载请注明出处:http://www.cnblogs.com/chruny/p/4872950.html
相关文章推荐
- Leetcode92: Swap Nodes in Pairs
- leetcode Populating Next Right Pointers in Each Node
- lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点
- node.js安装和使用步骤-windows
- Remove Nth Node From End of List
- 自制工具:迅速打开一个Node 环境的Playground
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- nodejs入门实践
- js下firstElementChild firstChild 以及childNodes和children方法
- Nodejs入门学习——fs.renameSync出错
- ReactPHP── PHP版的Node.js
- nodejs更改文件夹下的文件名
- NodeJs 中的Crypto 加密模块
- android GMS认证之testNoDebuggable
- node.js (1)安装与环境配置
- nodeclub route
- nodeclub models
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
- 在 Ubuntu 14.04/15.04 上配置 Node JS v4.0.0
- node.js调试