LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的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(交换链表中每两个相邻节点)的更多相关文章
- [leetcode]24. Swap Nodes in Pairs交换链表的节点
感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->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 ...
- Leetcode 24. Swap Nodes in Pairs(详细图解一看就会)
题目内容 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [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 ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]24. Swap Nodes in Pairs两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
随机推荐
- jmeter实现一次登录,多次业务请求(不同线程组间共享cookie和变量)
实现目的 很多时候,在进行性能测试时,需要先登录,然后再对需求的事务进行相关性能测试,此时的登录操作,并不在本次性能测试的范围内,所以我们只需要登录一次,然后获取登录成功后的cookie等,并将coo ...
- 题解 SP8284 WEIGHT - Weighted Sum
SP8284 WEIGHT - Weighted Sum 题意描述 给出长度为n(n<=1e6)的序列A, A中元素可能为正数,可为负数或0,.让你构造一个长度为n的序列W,给这些整数A赋权,使 ...
- Angular NgModule(模块)
NgModule 模块是Angular种一个重要的点,因为Angular的基本构造块就是NgModule.NgModule 会把相关的代码收集到一些功能集中,形成功能单元.在使用Angular CL ...
- 熟悉这几道 Redis 高频面试题,面试不用愁
1.说说 Redis 都有哪些应用场景? 缓存:这应该是 Redis 最主要的功能了,也是大型网站必备机制,合理地使用缓存不仅可以加 快数据的访问速度,而且能够有效地降低后端数据源的压力. 共享Ses ...
- linux下windows访问samba服务器
今天学习windows如何访问linux的共享文件. 开通samba服务,首先需要在linux下安装samba程序包,又再次使用光盘镜像挂载: [root@localhost home]# mount ...
- opencv:像素统计信息
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...
- 攻防世界 simple——js
simple_js [原理] javascript的代码审计 [目地] 掌握简单的javascript函数 [环境] windows [工具] firefox [步骤] 1.打开页面,查看源代码,可以 ...
- [C++_QT] Error: Not a signal or slot declaration
问题: 在Qt工程中添加了一个新的窗口之后 一直报错 如下 单单从错误描述上看 是缺少信号或者槽 但是我确定没有缺少啊 然后第二个错误显示了一个mox_xxxx文件 然后我就去那个目录下去找那个文件 ...
- ES6-对象的简写方式
var name = 'tom'; var age = 11; //es5定义对象 var obj = { name:name, age ...
- 《TCP/IP网络编程》读书笔记
1.Windows 下的 socket 程序和 Linux 思路相同,但细节有所差别(1) Windows 下的 socket 程序依赖 Winsock.dll 或 ws2_32.dll,必须提前加载 ...