Given a linked list, swap every two adjacent nodes and return its head. You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:          Given 1->2->3->4, you should return the list as 2->1->4->3.

将相邻的两个节点进行交换,在不交换值的前提条件下,只对节点指针进行交换。   时间复杂度为o(n),空间复杂度为O(1)。

思路: 可以利用链表中的哨兵机制来简化操作。具体操作步骤如下:

   

    

 class Solution(object):
def swapPairs(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
GuardNode = p = ListNode(0)
GuardNode.next = head while head and head.next:
tem = head.next # 指向第二个节点
head.next = tem.next # 第一个节点指向第三个节点
tem.next = head # 第二个节点指向第一个节点
p.next = tem # 哨兵指向反转后的第一个节点
p = head # 指向下两个待反转节点的前一个节点。
head = head.next # 指向下面即将反转的第一个节点 return GuardNode.next

【LeetCode每天一题】Swap Nodes in Pairs的更多相关文章

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

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

  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 题目整理-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 ...

  4. LeetCode(24) Swap Nodes in Pairs

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

  5. 【leetcode❤python】24. Swap Nodes in Pairs

    #-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):#     def __init ...

  6. leetcode个人题解——#24 Swap Nodes in Pairs

    因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...

  7. 乘风破浪:LeetCode真题_024_Swap Nodes in Pairs

    乘风破浪:LeetCode真题_024_Swap Nodes in Pairs 一.前言 这次还是链表的操作,不过我们需要交换链表奇数和偶数位置上的节点,因此要怎么做呢? 二.Swap Nodes i ...

  8. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

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

  10. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

    1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...

随机推荐

  1. juqery 判断所有input 不能为空 判断只能为数字 判断身份证号:18位和15位 判断是否银行卡号

    //jq 判断某字符串是否含有特殊符号 function CheckNum() { //定义数组保存特殊字符 var AllNumIsSame = new Array("’", & ...

  2. python3.6利用pyinstaller模块打包程序为.exe可执行程序

    步骤: 1.安装pyinstaller模块:(必须在联网情况下进行) 操作原理: python3.6已经自带了pip,所以只需要在cmd中执行 pip install pyinstaller 就可以安 ...

  3. Lucene.net(4.8.0) 学习问题记录六:Lucene 的索引系统和搜索过程分析

    前言:目前自己在做使用Lucene.net和PanGu分词实现全文检索的工作,不过自己是把别人做好的项目进行迁移.因为项目整体要迁移到ASP.NET Core 2.0版本,而Lucene使用的版本是3 ...

  4. c++基本函数

    std::abs 求绝对值函数 double abs (double x); float abs (float x); long double abs (long double x); std::sw ...

  5. java小tip

    //20181128 ·javaJDK源码在c盘java安装目录里jdk文件夹src.zip压缩包里 ·HashCode()返回的数可能是负数 ·内部类的优点:可以方便调用所在类的方法 ·接口中定义常 ...

  6. ado.net EF学习系列----深入理解查询延迟加载技术(转载)

    ado.net EF是微软的一个ORM框架,使用过EF的同学都知道EF有一个延迟加载的技术. 如果你是一个老鸟,你可能了解一些,如果下面的学习过程中哪些方面讲解的不对,欢迎批评指教.如果一个菜鸟,那我 ...

  7. [No0000123]WPF DataGrid Columns Visibility的绑定

    场景:根据配置文件显示DataGrid中的某些列. 问题:Columns集合只是DataGrid的一个属性,这个集合在逻辑树或视觉树中是看不到的,也不会继承DataContext属性. 方法一:对Da ...

  8. 【紫书】Ordering Tasks UVA - 10305 拓扑排序:dfs到底再输出。

    题意:给你一些任务1~n,给你m个数对(u,v)代表做完u才能做v 让你给出一个做完这些任务的合理顺序. 题解:拓扑排序版题 dfs到底再压入栈. #define _CRT_SECURE_NO_WAR ...

  9. express链接mysql, 用数据库连接池管理链接

    1.在API的开发当中,数据库的处理显得尤为重要,express 工程 链接mysql数据库有很好的模板可以借鉴. 1.1 创建数据库链接 新建一个DB目录,在DB目录下新建文件 db.js 内容如下 ...

  10. 内存管理 垃圾回收 C语言内存分配 垃圾回收3大算法 引用计数3个缺点

    小结: 1.垃圾回收的本质:找到并回收不再被使用的内存空间: 2.标记清除方式和复制收集方式的对比: 3.复制收集方式的局部性优点: https://en.wikipedia.org/wiki/C_( ...