[抄题]:

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. 集合总结二(LinkedList的实现原理)

    一.概述 先来看看源码中的这一段注释,我们先尝试从中提取一些信息: Doubly-linked list implementation of the List and Deque interfaces ...

  2. Linux eclipse 编译C++

    1.软件安装 2.新建C++工程 3.输入新建文件夹的名字 4.新建main.cpp文件 5.编辑main.cpp #include<iostream> int main(){ std:: ...

  3. 微信支付,退款时,出现了内部错误-网站中X509Certificate2加载证书时出错

    今天给阿里云,虚拟主机 网站配置了加密证书文件,用类X509Certificate2加载证书文件时,一直报出现了内部错误,但是Demo中用控制台程序加载证书没任何问题 读取证书文件的语句: X509C ...

  4. 用UpdateResource修改EXE文件图标(已修正)

    //请自行添加到 Type 处PICONDIRENTRY = ^ICONDIRENTRY;ICONDIRENTRY = packed record bWidth: Byte; bHeight: Byt ...

  5. 被称为“开发者神器”的GitHub,到底该怎么用?

    被称为“开发者神器”的GitHub,到底该怎么用? 原文:https://baijiahao.baidu.com/s?id=1594232691312740966&wfr=spider& ...

  6. git与github区别与简介

    From: https://blog.csdn.net/skyxmstar/article/details/65631658 git和github是两个完全不同的概念. git 是一个版本管理工具,是 ...

  7. jsp 进度条

    <html>  <head>  <title>进度条</title>  <style type="text/css">  ...

  8. MongoDB复制集技术

    复制集搭建 没毛病: https://www.cnblogs.com/nicolegxt/p/6841442.html?utm_source=itdadao&utm_medium=referr ...

  9. vue1 & vue2 数据驱动更新视图机制对比

    vue1 小粒度更新,精确追踪到数据变化所影响的dom变化,精确更新变化的dom 具体实现为,维护 observer watcher directive 三个类 ·observer负责监听数据变化,并 ...

  10. rpm软件包、yum软件仓库、systemd初始化进程

    rpm软件包.yum软件仓库.systemd初始化进程 作者:Eric 微信:loveoracle11g 红帽软件包管理器rpm (Redhat Package Manager) RPM会建立统一的数 ...