题目来源:

  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的更多相关文章

  1. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  2. 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 ...

  3. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  4. 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 ...

  5. 【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 ...

  6. 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-& ...

  7. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

    Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...

  8. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  9. LeetCode(24) Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  10. 024 Swap Nodes in Pairs 交换相邻结点

    给定一个链表,对每两个相邻的结点作交换并返回头节点.例如:给定 1->2->3->4,你应该返回 2->1->4->3.你的算法应该只使用额外的常数空间.不要修改列 ...

随机推荐

  1. php正则表达式的基本语法

    简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.我们可以在几乎所有的基于UNIX系统的工具中找到正则表达式的身影,例 如,vi编辑器,Perl或PHP脚本语言,以及awk或sed sh ...

  2. jar包问题

    解决方法: 1右键项目build path的lib的add jars添加进来

  3. 网页在Safari快速滚动和回弹的原理: -webkit-overflow-scrolling : touch;的实现

    现在很多for Mobile的H5网页内都有快速滚动和回弹的效果,看上去和原生app的效率都有得一拼. 要实现这个效果很简单,只需要加一行css代码即可: -webkit-overflow-scrol ...

  4. HEVC与VP9编码效率对比

    HEVC(High EfficiencyVideo Coding,高效率视频编码)是一种视频压缩标准,H.264/MPEG-4 AVC的继任者.目前正在由ISO/IEC MPEG和ITU-T VCEG ...

  5. Js闭包与循环

    目标:点击任何一个li,提示当前点击位置 <ul> <li>第1个</li> <li>第2个</li> <li>第3个</ ...

  6. jvm监控命令介绍

    1.jstack介绍 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在 ...

  7. C++读写文件的简单例子

    #include <iostream> #include <fstream> using namespace std; void main() { ofstream in; i ...

  8. Mac系统下下删除加锁文件方法|使用终端命令强制清除废纸篓中的文件

    链接地址1:http://jingyan.baidu.com/article/fdffd1f8e39403f3e98ca195.html 在Mac OS X下,无法删除的文件无外乎三种情况:1,文件( ...

  9. Android应用开发基础篇(10)-----Menu(菜单)

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/28/2372101.html 一.概述 Menu,简单来理解就是当你按下手机的“menu”键时所 ...

  10. Linux内核学习笔记-1.简介和入门

    原创文章,转载请注明:Linux内核学习笔记-1.简介和入门 By Lucio.Yang 部分内容来自:Linux Kernel Development(Third Edition),Robert L ...