24. Swap Nodes in Pairs 链表每2个点翻转一次
[抄题]:
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given1->2->3->4, you should return the list as2->1->4->3.
Note:
- Your algorithm should use only constant extra space.
- You may not modify the values in the list's nodes, only nodes itself may be changed.
[暴力解法]:
时间分析:
空间分析:dummy node,新建才是n,不新建就是1
[优化后]:
时间分析:
空间分析:recursive用的是stack, 空间恒定为n.
特例:尾递归是
[奇葩输出条件]:
[奇葩corner case]:
从节点非空就行了
[思维问题]:
不知道指针用什么顺序交换:
先外后里,外面的两点前后无所谓。
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
用一个cur做主节点 负责移动,一个first second分别做后面两个从节点(因此循环条件就是从节点非空?)
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 不知道指针的改变是按什么顺序的:先外面、后里面
- 不知道指针的改变怎么写:就和方程左边指向方程右边即可
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
学了很多:先外后里,左指右,从节点非空
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
//cc
if (head == null) return head; //ini: dummy node
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy; //while loop if the two nodes are not null
while (cur.next != null && cur.next.next != null) {
//initialize two ListNode
ListNode first = cur.next;
ListNode second = cur.next.next; //change the pointer
cur.next = second;
first.next = second.next;
second.next = first; //move the cur
cur = cur.next.next;
} return dummy.next;
}
}
24. Swap Nodes in Pairs 链表每2个点翻转一次的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- leetcode 24. Swap Nodes in Pairs(链表)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
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 (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 24. Swap Nodes in Pairs[M]两两交换链表中的节点
题目 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the va ...
随机推荐
- vue企业项目搭建过程(vue-cli脚手架超详细教程 傻瓜-入门)
vue作为现在主流的前端框架,有必要学习一下. vue的官方文档还是不错的,开源中文,一个爽字形容. 如果不是实际开发需要vue-cli构建项目,那么可以在加一个爽. 然而要构建的时候发现官方文档还是 ...
- laravel 使用 php artisan make:model到指定目录(controller同理)
在 \app\Models 目录下创建一个BusinessProduct模型文件 D:\htdocs\PHPTutorial\WWW\gf>php artisan make:model /Mod ...
- 1.4 安装Linux系统
按F2进入BIOS,设置通过[CD/ROM]启动,如果是真实计算机,安装完后还需要重新设置为[硬盘启动] 设置分区如下图所示:
- asp.net:mv4 FileResult在IE8中下载不显示文件名和扩展名而显示Action方法名了!
IE8下,用户点击下载文件,会发现文件类型失丢的问题,解决方案如下: //IE8下载时,只显示action的名字,没有文件名和后缀 @仰止网Simba //return File(bufferbyte ...
- JAVA 集合操作总结
1.Collection 1.基本操作 对集合的基础操作 1.boolean add(Object o) //添加对象到集合 2.boolean remove(Object o) //删除指定的对象 ...
- Office_PPT_让你一分钟完成上百张图片的快速保存
1 方式 修改PPT文件格式,由PPT修改为rar,再进行解压操作 进入到ppt->media中找到你在PPT为文件中使用的图片. 2 PPT北京图片下载网址 别样网:https://www.s ...
- 从Tomcat的处理web请求分析Java的内存模型
Tomcat作为一个java应用,同样是有主线程和子线程的.主线使用while(true)的方式一直循环,等待客户端来连接.一个客户端来了之后,就从线程池中拿一个线程来处理请求,如果没有配置线程池,就 ...
- Linux 入侵检测小结
Linux 入侵检测小结 0x00 审计命令 在linux中有5个用于审计的命令: last:这个命令可用于查看我们系统的成功登录.关机.重启等情况:这个命令就是将/var/log/wtmp文件格式 ...
- HTTPS如何保证数据传输的安全性 -- 结合加密
什么是HTTPS: HTTP就是我们平时浏览网页时候使用的一种协议 HTTP协议传输的数据都是未加密的,也就是明文的,因此使用HTTP协议传输隐私信息非常不安全.为了保证这些隐私数据能加密传输,于是网 ...
- rabbitmq (三) 发布/订阅
rabbitmq的目的并不是让生产者把消息直接发到队列里面去, 这样不能实现解耦的目的,也不利于程序的扩展. 所以就有交换机(exchanges)的概念. 交换机有几种类型:direct, topic ...