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. window的cmd使用

    有时候安装软件也需要使用cmd,如果要进入目前所在盘符的其他路径用cd命令可进入,但如果从c盘进入d盘等,是不用cd命令的. 如目前在c盘的任意目录,需要切换到d盘的根目录,用:“d:”命令(不含引号 ...

  2. UIScrollView _getDelegateZoomView bug 经历

    [UIScrollView _getDelegateZoomView] UIKit -[UIScrollView_offsetForCenterOfPossibleZoomView:withIncom ...

  3. 转载:mybatis踩坑之——foreach循环嵌套if判断

    转载自:作者:超人有点忙链接:https://www.jianshu.com/p/1ee41604b5da來源:简书 今天在修改别人的代码bug时,有一个需求是在做导出excel功能时,mybatis ...

  4. Codeforces 1132C - Painting the Fence - [前缀和优化]

    题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...

  5. [No0000CB]如何在命令行(cmd)通过TCP/IP端口(port)查询所在的进程号(pid)或进程名称,并终止该进程

      1)首先查找占用某个端口的进程PID netstat -ano | findstr [port] 2)根据该进程pid查询进程名称或标题,确认那个程序在占用该端口 tasklist /v | fi ...

  6. strlen函数的汇编实现分析

    [原创][老刘谈算法001]这位运算玩的真溜—strlen函数的汇编实现分析-『密码算法』-看雪安全论坛 https://bbs.pediy.com/thread-229243.htm

  7. [DPI][suricata] suricata 配置使用

    前文: [DPI] suricata-4.0.3 安装部署 至此, 我们已经拥有了suricata可以运行的环境了. 接下来,我们来研究一下它的功能, 首先,分析一下配置文件: /suricata/e ...

  8. 游戏引擎架构 && windows 核心编程

    欲想正人,必先正己. 静坐当思己过,闲谈莫论人非. 垂直同步的作用: 为避免画面撕裂,许多渲染引擎会在交换缓冲区之前,等待显示器的垂直区间消隐,即电子枪重归屏幕上角的时间. 高分辨率计时器的时间漂移 ...

  9. javaweb连接数据库并完成增删改查

    一.连接数据库 1.mysql数据库的安装和配置 在网上找到了篇关于mysql的安装详细说明,供读者自己学习 https://www.jb51.net/article/23876.htm 2.mysq ...

  10. LeetCode 766 Toeplitz Matrix 解题报告

    题目要求 A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element. Now ...