题目内容

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

You may not modify the values in the list’s nodes, only nodes itself may be changed.
大概意思是说每两个节点为一组交换位置。只使用常量空间,不能修改链表的值,只能修改链表的指针

解法(迭代)

class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode *dummy = new ListNode(-1), *pre = dummy;
dummy->next = head;
while (pre->next && pre->next->next) {
ListNode *t = pre->next->next;
pre->next->next = t->next;
t->next = pre->next;
pre->next = t;
pre = t->next;
}
return dummy->next;
}
};

该代码已经是非常经典了,但乍一看还是有点晕,我们来对代码进行一步步的解析。

可以看出该代码实现了两个相邻节点的交换,最后并将pre指向了下一个节点,然后继续该过程

递归解法

class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *t = head->next;
head->next = swapPairs(head->next->next);
t->next = head;
return t;
}
}; 

Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)的更多相关文章

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

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

    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. You may not modify the value ...

  4. Java [leetcode 24]Swap Nodes in Pairs

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

  5. Leetcode 24——Swap Nodes in Pairs

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

  6. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

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

  7. (链表 递归) leetcode 24. Swap Nodes in Pairs

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

  8. [leetcode]24. Swap Nodes in Pairs交换节点对

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

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

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

随机推荐

  1. Day15_阿里短信

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 1.开通 ...

  2. Python continue语句

    Python continue语句: 当执行到 continue 语句时,将不再执行本次循环中 continue 语句接下来的部分,而是继续下一次循环. lst = [7,8,9,4,5,6] for ...

  3. MySQL三种InnoDB、MyISAM和MEMORY存储引擎对比

    什么是存储引擎? MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术,你能 ...

  4. PHP highlight_file() 函数

    实例 对测试文件("test.php")进行 PHP 语法高亮显示: <html><body><?phphighlight_file("te ...

  5. PHP str_word_count() 函数

    实例 计算字符串 "Hello World!" 中的单词数: <?php高佣联盟 www.cgewang.comecho str_word_count("Hello ...

  6. PDOStatement::closeCursor

    PDOStatement::closeCursor — 关闭游标,使语句能再次被执行.(PHP 5 >= 5.1.0, PECL pdo >= 0.9.0) 说明 语法 bool PDOS ...

  7. P4221 [WC2018]州区划分 无向图欧拉回路 FST FWT

    LINK:州区划分 把题目中四个条件进行规约 容易想到不合法当前仅当当前状态是一个无向图欧拉回路. 充要条件有两个 联通 每个点度数为偶数. 预处理出所有状态. 然后设\(f_i\)表示组成情况为i的 ...

  8. SparkSQL JDBC和JDBCServer区别

    注意SparkSQL JDBC和SparkSQL JDBCSever是完全两种不同的场景. SparkSQL JDBC SparkSQL可以使用JDBC的方式访问其他数据库,和普通非spark程序访问 ...

  9. maven中的陌生单词

    有个单词记不住啊: artifact:人工制品,手工艺品,加工品; 石器; 词根:fac,fact,fect,fic,fig=make,do,表示“做,制作”   因此 art i fact 意思很好 ...

  10. 云服务器远程连接mysql数据库

    首先需要在云服务器上,下载安装好mysql与Navicat. mysql下载好以后,打开云端的开始,找到mysql的命令窗,进入输入自己的mysql密码,稍等片刻进入mysql数据库 进入之后输入下列 ...