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.
特例:尾递归是1
[奇葩输出条件]:
[奇葩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) {//ccif (head == null) return head; //ini: dummy nodeListNode dummy = new ListNode(0);dummy.next = head;ListNode cur = dummy;//while loop if the two nodes are not nullwhile (cur.next != null && cur.next.next != null) {//initialize two ListNodeListNode first = cur.next;ListNode second = cur.next.next;//change the pointercur.next = second;first.next = second.next;second.next = first;//move the curcur = cur.next.next;}return dummy.next;} }
View Code
转载于:https://www.cnblogs.com/immiao0319/p/9397580.html
24. Swap Nodes in Pairs 链表每2个点翻转一次相关推荐
- 【重点】LeetCode 24. Swap Nodes in Pairs
LeetCode 24. Swap Nodes in Pairs 参考网址:http://www.cnblogs.com/grandyang/p/4441680.html 此题算是链表中比较考察细节的 ...
- 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- leetcode 24. Swap Nodes in Pairs | 24. 两两交换链表中的节点(Java)
题目 https://leetcode.com/problems/swap-nodes-in-pairs/ 题解 两两翻转,链表题 so easy /*** Definition for singly ...
- LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)
翻译 给定一个链表,调换每两个相邻节点,并返回其头部.例如, 给定 1->2->3->4, 你应该返回的链表是 2->1->4->3.你的算法必须使用唯一不变的空间 ...
- [leetcode] 24. Swap Nodes in Pairs
题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- Leetcode 24. Swap Nodes in Pairs
不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 本题很简单,大致翻译过来的意思是将单链表相邻节点互换节点位置.额外的要求是使用O(1)空间,以及不修改节点值,仅对节 ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上图所示,递归 ...
- 【LeetCode】24. Swap Nodes in Pairs
[题目] 给一个链表,更换相邻结点的位置,要求不能修改结点的值,只能修改结点位置 比如: 给出1->2->3->4, 返回 2->1->4->3. [思路] 该题考 ...
最新文章
- MYSQL注入天书之服务器(两层)架构
- Linux curl API 使用 C语言
- 光敏电阻检测12528
- myeclipse2014新感悟
- Linux内核源码学习
- mybatis mysql 模糊查询语句_mybatis+Spring mysql的模糊查询问题
- 【离散数学·图论】关于哈密顿图的判别条件总结
- 【腾讯TMQ】远程移动测试平台对比分析
- 北大核心期刊目录2021年 计算机类
- 【CF633H】Fibonacci-ish II 莫队+线段树
- app逆向工程之apk回编译安装之后背景音乐出不来
- 用命令备份与还原网络设置
- 递四方(4PX)一面
- 6款炫酷的HTML5 3D特效源码
- 2023,VC投资的分水岭
- 如何升级Python的pip?
- 京东 App 鸿蒙版上架华为应用商店
- 悬崖勒马回头是岸——关于玩王者荣耀游戏的一些想法
- 论文阅读笔记1:EKT: Exercise-aware Knowledge Tracing for Student Performance Prediction
- 经纬度度与度分格式转化
热门文章
- Java IO 之 InputStream源码
- kFreeBSD有活过来的迹象?UbuntuBSD
- Linux下MySql插入汉字报错解决(/etc/my.cnf不存在)
- lintcode :Partition List 链表划分
- 针对应用开发者的几点建议
- questions for the exploitation of technologies
- navigating the online library
- what to improve on my case study?
- how to improve your ielts score on the exam?
- Heinz College of Information Systems and Public Policy Carnegie Mellon University