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. 【react】---手动封装一个简易版的redux---【巷子】

    export let createStore = (reducer)=>{ //定义默认的state let state = {}; //定义默认的action let actionTypes ...

  2. 栈的C语言实现

    在C++中,可以直接使用std::stack C语言实现如下: stack.c /** * @file stack.c * @brief 栈,顺序存储. * * * */ #include <s ...

  3. Dapper的数据库连接管理(打开、关闭)

    Dapper对于数据库连接的管理:如果已经打开,它会关闭连接.如果你只是做一个快速查询-让Dopter自己处理它. 如果你做了很多事情,你应该自己打开连接,并在最后关闭连接,所有的查询在中…只是从效率 ...

  4. Warning: Failed to halt at after bootloader, forced stop at

    该错误证实是因为 cc2650 SW下载模式,芯片复位引脚未接出来导致,芯片复位必须和下载器保持良好连接

  5. linux文件系统扩展属性

    翻译自man手册,水平有限,有错还望不吝指出.... 扩展属性是与文件和目录相关的name:value对,用来提供文件系统的一些附加功能,例如ACL.对文件或是目录拥有读权限的用户可以看到其扩展属性. ...

  6. 如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件。

    如果是多个 c 代码的源码文件,编译方法如下: $ gcc test1.c test2.c -o main.out $ ./main.out test1.c 与 test2.c 是两个源代码文件.

  7. php之函数

    scope(空间) unpack (解压) Traversable (穿越) performance(性能) experiment (检验) properties (属性) trailing (尾随) ...

  8. php值callback类型和匿名函数(闭包)

    callback.callable类型 自PHP5.4起可以使用callable类型制定回调类型callback. 本文档基于同样理由使用callback类型信息. 一些函数如call_user_fu ...

  9. SR锁存器

    CRM(临界连续模式)BOOST PFC 电路控制系统 SR锁存器 S和R都等于0的时候为什么有两个不同的Q?正因为这样才叫锁存器.Q’是Q的取反,不可能相同.Q*和Q‘不一样.Q是Q*的前一个状态. ...

  10. 查找->动态查找表->B+树(无代码)

    文字描述 B+树定义 B+树是应文件系统所需而出的一种B-树的变型树.一棵m阶的B+树和m阶的B-树的差异在于: (1)有n棵子树的结点中含有n个关键字 (2)所有的叶子结点中包含了全部关键字的信息, ...