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 ...
随机推荐
- 【java多线程】队列系统之DelayQueue源码
一.延迟队列 延迟队列,底层依赖了优先级队列PriorityBlockingQueue 二.延迟队列案例 (1)延迟队列的任务 public class DelayTask implements De ...
- session token两种登陆方式
Session 和 Token 其实Session和Token总体上还是很相似的,但是也有以下区别: 1. 过期时间:Session的过期时间存在cookie的Max-age字段,Token的过期时间 ...
- PHP-ML机器学习库之安装篇
1.PHP-ML库安装要求:PHP>=7.1 2.切换到项目的跟目录下,使用composer进行安装:composer require php-ai/php-ml 安装完成后的目录如下: 新建测 ...
- 轻量应用服务器安装 phpMyAdmin
第一步:在phpMyAdmin官方网站http://www.phpmyadmin.net/downloads/下载源码包并解压 cd /usr/local/src wget https://files ...
- C/C++中的volatile简单描述
首先引入一篇博客: 1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 ...
- Python打包之pyinstaller
Pyinstaller 进行对应目录下的打包 执行完后请注意建议将该m2的拷贝走 加了参数-w后不会出现黑色控制台的窗子 常用参数说明: –icon=图标路径 -F 打包成一个exe文件 -w 使用窗 ...
- Scrapy学习篇(十三)之scrapy+selenum获取网站cookie并保存带本地
参考:https://www.cnblogs.com/small-bud/p/9064674.html 和selenium登录51job的例子
- 单链表查找第i个节点
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct Node { char ...
- 某大型跨境电商JVM调优总结
前提:某大型跨境电商业务发展非常快,线上机器扩容也很频繁,但是对于线上机器的运行情况,特别是jvm内存的情况,一直没有一个统一的标准来给到各个应用服务的owner.经过618大促之后,和运维的同学讨论 ...
- [Unity优化]UI优化(二):Mask组件分析
参考链接: https://www.sohu.com/a/211665096_99940808 1.Mask组件实现原理 使用模板测试,一方面使Mask对象所在区域的模板缓冲值置为1,另一方面使被Ma ...