[LeetCode]题解(python):024-Swap Nodes in Pairs
题目来源:
https://leetcode.com/problems/swap-nodes-in-pairs/
题意分析:
给定一个链表,每两个相邻节点就行交换。比如1->2->3->4,得到2->1->4->3。要求不能改变节点的值,不能新建链表。
题目思路:
这题是考链表的操作。首先建立一个头节点,将头节点指向第二个节点,然后再指向第一个节点,最后指向第三个节点,然后指针跳到第二个节点重复。
代码(python):
# 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
转载请注明出处:http://www.cnblogs.com/chruny/p/4872950.html
[LeetCode]题解(python):024-Swap Nodes in Pairs的更多相关文章
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- LeetCode 024 Swap Nodes in Pairs
题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 【LeetCode】024. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Java for LeetCode 024 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- leetcode第23题--Swap Nodes in Pairs
Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...
- LeetCode(24) Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- 024 Swap Nodes in Pairs 交换相邻结点
给定一个链表,对每两个相邻的结点作交换并返回头节点.例如:给定 1->2->3->4,你应该返回 2->1->4->3.你的算法应该只使用额外的常数空间.不要修改列 ...
随机推荐
- S3C6410嵌入式应用平台构建(一)
[2014-4/8~4/10]目前我们已经积累一定的嵌入式相关知识,对嵌入式的架构及开发过程有了大体了解,唯一缺的就是实践,通过自己的分析搭建自己的嵌入式系统.下面,我将从此处开始记录我和我同学一起分 ...
- Echoprint系列--Android编译与调用
在Echoprint系列--编译中编译了源代码,这次将Echoprint移植到Android平台并測试识别歌曲功能. 一.编译库 1.环境准备 Android NDK,我的是android-ndk-r ...
- Android中各种Adapter的使用方法
1.概念 Adapter是连接后端数据和前端显示的适配器接口.是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都须要用到Adapter.例如以下 ...
- php 求水仙花数优化
水仙花数是指一个n位数(n>=3),它每一个位上数字的n次幂之和等于它本身,n为它的位数.(比如:1^3+5^3+3^3 = 153) 水仙花数又称阿姆斯特朗数. 三位的水仙花数有4个:153, ...
- linux核心之进程管理
进程就是处于执行期的程序(目标码存放在某中介质上).进程并不仅仅局限于一段可执行程序代码,通常还包括其他资源,例如打开的文件,挂起的信号,内核内部数据,处理器状态,一个或多个具有内存映射的内存地址空间 ...
- MVC过滤器基本使用
Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...
- Android ListView 删除动画
Android 的ListView在删除条目时,被删除的条目直接消失,比较生硬,在此实现一下删除动画,大家一起探讨:主要实现原理即是通过Animator来实现被删除条目的动画效果,然后在动画结束时通过 ...
- maxContainerCapability 设置不足
异常: REDUCE capability required is more than the supported max container capability in the cluster. K ...
- C#_C++_SDK_WM_KEYDOWN人物卡顿延迟解决方法
提问者采纳 由Keydown和keyup事件组合,keyDown来判定按下,此时开始移动,KeyUp判定松开,这样可行否? 追问 这是我一开始的写法,但就是因为 键盘重复延迟 导致keydown后会有 ...
- win7 64位 TortoiseSVN-1.8.4客户端安装
下载地址链接:http://pan.baidu.com/s/1nukeBVz 密码:tc79 (32 64位都有,注意区分) next一路安装 安装好后,在需要和服务器同步的文件夹图标上--鼠标右键- ...