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 ...
随机推荐
- ios-密码判断
我们经常在项目时有涉及到用户或是手机号登录,这时一般会配合密码才能登录成功. 下面发一些关于手机和密码形式的判断: - (void)registButtonClick:(id)sender { )// ...
- string函数的一些实现
/************************************************************************* > File Name: test.cpp ...
- zombodb 几点说明
内容来自官方文档,截取部分 默认es 索引的副本为0 这个参考可以通过修改索引,或者在创建的时候通过with 参数指定,或者通过pg 的配置文件中指定 索引更多的列以为这使用了更多的es 能力 索引的 ...
- spring IOC 和AOP 方面
spring 的2大核心 是Ioc 和 aop spring的依赖注入:在程序运行期间,由外部容器动态的将依赖对象注入到组件中 IOC: 实例化spring容器的二种方法 第一种:在类路径下寻找配 ...
- Flutter 学习资料
Flutter 学习资料: 学习资料 网址 Flutter 中文网 https://flutterchina.club/ <Flutter实战>电子书 https://book.flutt ...
- mongodb集群配置副本集
测试环境 操作系统:CentOS 7.2 最小化安装 主服务器IP地址:192.168.197.21 mongo01 从服务器IP地址:192.168.197.22 mongo02 从服务器IP地址: ...
- DHCP的搭建
挂载光盘 yum –y install dhcp cat /etc/dhcp/dhcpd.conf 配置文件到 /usr/share/doc/dhcp*/dhcpd.conf.sample 这是dhc ...
- jenkins构建触发器详解-不登录触发远程构建详解
利用jenkins的远程构建功能,我们可以使用任何脚本,甚至定制一个Web页来控制Job的执行,但是远程构建你如果直接使用的话,老是需要登录才能执行,如何避免登录?稍微折腾了一下,调通了. 1.首先去 ...
- Container 、Injection
Container: Linux容器作为一类操作系统层面的虚拟化技术成果,旨在立足于单一Linux主机交付多套隔离性Linux环境.与虚拟机不同,容器系统并不需要运行特定的访客操作系统.相反,容器共享 ...
- mkimage command not found – U-Boot images will not be built
ubuntu 14.04 64位系统编译Linux kernel时提示: “mkimage” command not found – U-Boot images will not be built 按 ...