[抄题]:

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.

特例:尾递归是

[奇葩输出条件]:

[奇葩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) {
//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个点翻转一次的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  2. 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 ...

  3. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  4. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  5. 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 ...

  6. 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 ...

  7. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. java初始重点语法

    第三章 if基本语法: if(条件){// 表达式 // 代码块 } eg: int a = 10; if(a > 1){ System.out.println("内容"); ...

  2. PythonStudy——列表操作 List operatio

    # 1.列表的增删改查 ls = [1, 2, 3] # 查 print(ls) print(ls[1]) # 增 ls.append(0) # 末尾增 print(ls) ls.insert(1, ...

  3. 推荐一些关于学习Html Css和Js的书吗?

    前端易学易懂,随着移动互联网的日益兴起,it行业对于前端的需求也在不断的提高,那么从前端小白修炼成为前端大神的这个过程之中,一些必备的枕边书也是必不可少的. 第一本,入门<Head first ...

  4. java_初始化器

    1. 执行的顺序 package java20180129_1; public class Demo { // instance variable initializer 实例变量初始化器 Strin ...

  5. 前端 --- 5 BOM 和 DOM

    一.BOM BOM(Browser Object Model)是指浏览器对象模型, 它使 JavaScript 有能力与浏览器进行“对话”. 1. window 对象 一些常用的Window方法: ( ...

  6. awk命令过滤tomcat的访日日志中IP地址

    1. 命令如下 批量过滤日志文件,grep -v是要排除10网段开头的IP地址 sort会自动按ip排序 uniq -c去重并计数 sort -n 按数值从小到大排序 [root@linux-node ...

  7. Markdown语法说明(转)

    Markdown语法说明(转) Markdown创始人John Gruber的语法说明 附上本文链接 NOTE: This is Simplelified Chinese Edition Docume ...

  8. problem:vue组件局部刷新,在组件销毁(destroyed)时取消刷新无效问题

    场景: 一个群发消息列表(数组) 列表下有多条消息(元素) 每条正在发送的消息数据状态需要实时刷新,发送完成时需要显示成功提示符合且不需要刷新,然后3秒消失.首次显示列表时,已经成功的状态不显示这个成 ...

  9. centos7-软件安装-redis3.2

    wget方式下载redis3.2 wget http://download.redis.io/releases/redis-stable.tar.gz 命令行下载redis,此命令会保存redis至当 ...

  10. [转][C#]Linq 的扩展方法

    public static class LinqEx { public static IEnumerable<TResult> LeftExcludingJoin<TSource, ...