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. flask下载文件中文IE,Edge,Safari文件名乱码

    flask(0.11.2)+python3.6 兼容各个主流浏览器,已经过各种测试(chrome,firefox,safari,IE,Edge) quote是将文件名urlencode化,然后以适应E ...

  2. eclipse下配置Spring环境

    工具: jdk1.8 win10 spring5.0 1.准备工作:下载Spring开发应用的插件,api 1.spring插件包:springsource-tool-suite-3.9.4.RELE ...

  3. 使 用 Jquery 全选+下拉+单选+事件+挂事件

    先引用Jquery代码包 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  4. linux 下安装 redis

    https://redis.io/ 1.下载 $ cd /usr/local/ $ wget http://download.redis.io/releases/redis-4.0.7.tar.gz ...

  5. keil 生成 bin文件

    D:\Keil_v5\ARM\ARMCC\bin\fromelf.exe --bin -o  $L@L.bin #L 这个就是keil的默认安装路径

  6. Codeforces 219C - Color Stripe - [DP]

    题目链接:http://codeforces.com/problemset/problem/219/C 题意: 给你 $n$ 个方块排成水平一排,每个方块都涂上 $k$ 种颜色中的一种.要求对尽量少的 ...

  7. shell脚本之使用sed和awk进行文本处理

    Shell这种脚本语言特点是,结果松散,场景复杂,针对于一些参数都有特殊意义.针对于大部分工程师而言,使用中的情况是你可能会经常忘记参数或其意义,使你不得不查阅man或网上寻求帮助.此篇文档作用就是在 ...

  8. Django:视图views(一)

    1.环境搭建 在django中,视图负责与web请求进行交互 视图本质上是一个Python函数,定义在booktest/views.py.通过django1/urls.py路由到该视图中. 首先经过创 ...

  9. Boost 常用的库

    boost是一系列C++模板库组成的免费,可移植,开源的程序库.网络上关于boost的文章已经很多.     这里摘记一些库的信息,供自己日后参考. 0.foreach - BOOST_FOREACH ...

  10. Python判断字符串是否为字母或者数字

    严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...