[抄题]:

Given a linked list, swap every two adjacent nodes and return its head.

Example:

Given 1->2->3->4, you should return the list as 2->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分别做后面两个从节点(因此循环条件就是从节点非空?)

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 不知道指针的改变是按什么顺序的:先外面、后里面
  2. 不知道指针的改变怎么写:就和方程左边指向方程右边即可

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼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个点翻转一次相关推荐

  1. 【重点】LeetCode 24. Swap Nodes in Pairs

    LeetCode 24. Swap Nodes in Pairs 参考网址:http://www.cnblogs.com/grandyang/p/4441680.html 此题算是链表中比较考察细节的 ...

  2. 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. leetcode 24. Swap Nodes in Pairs | 24. 两两交换链表中的节点(Java)

    题目 https://leetcode.com/problems/swap-nodes-in-pairs/ 题解 两两翻转,链表题 so easy /*** Definition for singly ...

  4. LeetCode 24 Swap Nodes in Pairs(交换序列中的结点)

    翻译 给定一个链表,调换每两个相邻节点,并返回其头部.例如, 给定 1->2->3->4, 你应该返回的链表是 2->1->4->3.你的算法必须使用唯一不变的空间 ...

  5. [leetcode] 24. Swap Nodes in Pairs

    题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  6. Leetcode 24——Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  7. Leetcode 24. Swap Nodes in Pairs

    不定期更新leetcode解题java答案. 采用pick one的方式选择题目. 本题很简单,大致翻译过来的意思是将单链表相邻节点互换节点位置.额外的要求是使用O(1)空间,以及不修改节点值,仅对节 ...

  8. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上图所示,递归 ...

  9. 【LeetCode】24. Swap Nodes in Pairs

    [题目] 给一个链表,更换相邻结点的位置,要求不能修改结点的值,只能修改结点位置 比如: 给出1->2->3->4, 返回 2->1->4->3. [思路] 该题考 ...

最新文章

  1. MYSQL注入天书之服务器(两层)架构
  2. Linux curl API 使用 C语言
  3. 光敏电阻检测12528
  4. myeclipse2014新感悟
  5. Linux内核源码学习
  6. mybatis mysql 模糊查询语句_mybatis+Spring mysql的模糊查询问题
  7. 【离散数学·图论】关于哈密顿图的判别条件总结
  8. 【腾讯TMQ】远程移动测试平台对比分析
  9. 北大核心期刊目录2021年 计算机类
  10. 【CF633H】Fibonacci-ish II 莫队+线段树
  11. app逆向工程之apk回编译安装之后背景音乐出不来
  12. 用命令备份与还原网络设置
  13. 递四方(4PX)一面
  14. 6款炫酷的HTML5 3D特效源码
  15. 2023,VC投资的分水岭
  16. 如何升级Python的pip?
  17. 京东 App 鸿蒙版上架华为应用商店
  18. 悬崖勒马回头是岸——关于玩王者荣耀游戏的一些想法
  19. 论文阅读笔记1:EKT: Exercise-aware Knowledge Tracing for Student Performance Prediction
  20. 经纬度度与度分格式转化

热门文章

  1. Java IO 之 InputStream源码
  2. kFreeBSD有活过来的迹象?UbuntuBSD
  3. Linux下MySql插入汉字报错解决(/etc/my.cnf不存在)
  4. lintcode :Partition List 链表划分
  5. 针对应用开发者的几点建议
  6. questions for the exploitation of technologies
  7. navigating the online library
  8. what to improve on my case study?
  9. how to improve your ielts score on the exam?
  10. Heinz College of Information Systems and Public Policy Carnegie Mellon University