题目来源:

  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. .NET(C#):觉察XML反序列化中的未知节点

    原文 www.cnblogs.com/mgen/archive/2011/12/12/2284554.html 众所周知XML是可以扩展的,XML的元素可以靠名称识别而不是只按照未知识别.在 XML反 ...

  2. CountDownLatch和CyclicBarrier区别及用法的demo

    javadoc里面的描述是这样的. CountDownLatch: A synchronization aid that allows one or more threads to wait unti ...

  3. UVA - 10129Play on Words(欧拉路)

    UVA - 10129Play on Words Some of the secret doors contain a very interesting word puzzle. The team o ...

  4. Html 语法学习笔记二

    1.图像标签(<img>)和源属性(Src) 在 HTML 中.图像由 <img> 标签定义.        <img> 是空标签,意思是说,它仅仅包括属性,而且没 ...

  5. Amazon S3 API

    一.概述 Amazon s3,全称为Amazon Simple Storage  Service.EC2和S3是Amazon最早推出的两项云服务. REST,这也是比较火的一种Web服务架构.简单来说 ...

  6. jar 命令使用详解

    各位看官,由于本人首次学习java,对编译java 还有很多问题. jar命令详解 本人总结 jar   -c [创建档案]  v[输出详细信息]  m[添加清单文件] f[指定文件名]     主要 ...

  7. 传统web和mvc的区别

  8. MVC过滤器基本使用

        Action过滤器 /// <summary> /// 执行代码前执行 /// </summary> /// <param name="filterCo ...

  9. spring jar包冲突

    在用Spring+Hibernate做项目时候遇到java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit 网上查得答案 环境 ...

  10. JavaSE复习日记 : 循环语句(for/while/do while)

    /* * 循环语句(for循环,while和do while循环) */ /* * for循环语句 * * for循环语法: * for (表达式1;表达式2;表达式3 ){ * java语句 * } ...