题意:交换链表中每两个相邻节点,不能修改节点的val值。

分析:递归。如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> next)),则接下来,只需交换前两个节点,再将第一个节点的next指向“以第三个结点为头结点的链表两两交换后”的链表。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL || head -> next == NULL) return head;
ListNode *second = head -> next;
ListNode *third = head -> next -> next;
second -> next = head;
head -> next = swapPairs(third);
return second;
}
};

LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)的更多相关文章

  1. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  2. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

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

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

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

  5. Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)

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

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

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

  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. For example, Given 1->2-& ...

  9. [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点

    Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...

随机推荐

  1. VSCode的安装和使用

    VSCode的安装和使用 1.先到下载地址选择适合自己系统的VSCode安装软件 https://code.visualstudio.com/#alt-downloads 以上分别是Windows , ...

  2. zol验证码抓包

    http://www.zol.com/detail/lcd/samsung/25254662.html?zp_from=pro_price_pricenode 网站’ http://city.zol. ...

  3. 萌新深度学习与Pytorch入门记录(一):Win10下环境安装

    深度学习从入门到入土,安装软件及配置环境踩了不少坑,过程中参考了多处博主给的解决方法,遂整合一下自己的采坑记录. (若遇到不一样的错误,请参考其他博主答案解决) 笔者电脑系统为win10系统,在此环境 ...

  4. 201771010135 杨蓉庆《面对对象程序设计(java)》第十六周学习总结

    1.实验目的与要求 (1) 掌握线程概念: (2) 掌握线程创建的两种技术: (3) 理解和掌握线程的优先级属性及调度方法: (4) 掌握线程同步的概念及实现技术: 一.理论知识 ⚫ 线程的概念 (1 ...

  5. ajax循环展示某段代码

    ajax内定义function,根据条件递归调用即可. success: function(data){ if (dataList[i].subModuleList){ sublist(dataLis ...

  6. [1/100]Python安装

    Python官方版本下载地址: https://www.python.org/downloads/release (上图为3.8.2网络安装版) 默认安装: pip Python安装模块 tkinte ...

  7. netty学习1

    1.IO java 自带IO流 1)同步处理方式,一个请求占用一个线程,高并发常见出现问题 2.java nio 实在jdk1.4版本上新添加的new IO / 非阻塞IO 可多路并发 3.Netty ...

  8. How2j学习java-2、用命令行中编写第一个 JAVA 程序

    真正在工作中开发 java 应用都会使用eclipse,myeclipse, IntelliJ等等 使用最原始的命令行方式来执行Hello World 1.准备项目目录 在e: 创建一个project ...

  9. laravel 动态属性

    我们在 User 模型里定义了关联方法 followings() 关联关系定义好后,我们就可以通过访问 followings 属性直接获取到关注用户的 集合.这是 Laravel Eloquent 提 ...

  10. Cisco AP-格式化AP

    故障情况:APC11-AP04#sho capwap ip config LWAPP Static IP ConfigurationIP Address         172.17.239.204I ...