【LeetCode每天一题】Swap Nodes in Pairs
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的更多相关文章
- leetcode第23题--Swap Nodes in Pairs
Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...
- 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 ...
- 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 ...
- LeetCode(24) Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- 【leetcode❤python】24. Swap Nodes in Pairs
#-*- coding: UTF-8 -*- # Definition for singly-linked list.# class ListNode(object):# def __init ...
- leetcode个人题解——#24 Swap Nodes in Pairs
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...
- 乘风破浪:LeetCode真题_024_Swap Nodes in Pairs
乘风破浪:LeetCode真题_024_Swap Nodes in Pairs 一.前言 这次还是链表的操作,不过我们需要交换链表奇数和偶数位置上的节点,因此要怎么做呢? 二.Swap Nodes i ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
随机推荐
- 重装win7,win10,Ubuntu
使用的软件多了,大了,感觉用起来很卡,快就是生产力,一定要重视.就去维修电安装了120G的固态硬盘,以前所安装的软件都没有了,距上一次重装快一年了,上次修了风扇,加了4G内存条.现在很多东西也一并删除 ...
- Java面向对象进阶篇(抽象类和接口)
一.抽象类 在某些情况下,父类知道其子类应该包含哪些方法,但是无法确定这些子类如何实现这些方法.这种有方法签名但是没有具体实现细节的方法就是抽象方法.有抽象方法的类只能被定义成抽象类,抽象方法和抽象类 ...
- WebApi路由
路由分为两种模式:模板路由和特性路由. 模板路由: 模板路由是ASP.NET Web API默认提供的路由.模板路由使用前需要定义路由模板.如下面默认的路由模板: 默认路由的URL格式是api/{co ...
- ABP之事件总线(5)
前面已经对Castle Windsor的基本使用进行了学习,有了这个基础,接下来我们将把我们的事件总线再次向ABP中定义的事件总线靠近.从源码中可以知道在ABP中定义了Dictionary,存放三种类 ...
- windous----常用命令集合
Windous常用命令 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程 ...
- ubuntu16.04 ROS环境下配置和运行SVO
ubuntu16.04 ROS环境下配置和运行SVO https://blog.csdn.net/nnUyi/article/details/78005552
- C# chart 编程教程
http://www.gcpowertools.com.cn/docs/ComponentOne/WinFormChart/#!Documents/_41.htm
- Django:模型model和数据库mysql(二)
上一篇把简单的模型与数据库的搭建写了一遍,但模型中有很多深入好用的写法补充一下. 同样的栗子,建立新的模型与数据库来写一写 1.依然是搭建环境 >>>django-admin sta ...
- Vue的自动化测试
前言 为什么我们需要测试? 让产品可以快速迭代,同时还能保持高质量 -- 阮一峰 持续集成是什么? 对于一些相对稳定的系统级别页面,自动化测试在提高测试的效率的方面起到非常重要的作用.前端的自动化测试 ...
- python3读取excel数据
import xlrd worksheet = xlrd.open_workbook('XXXX.xlsx') #打开excel文件 sheet_names= worksheet.sheet_na ...