LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值。
思路:迭代法和递归法都容易写,就写个递归的了。
4ms
/**
* 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 || !head->next ) return head; ListNode *t=head->next;
head->next=swapPairs(t->next);
return t->next=head, t;
} };
AC代码
LeetCode Swap Nodes in Pairs 交换结点对(单链表)的更多相关文章
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- LeetCode: Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- 024 Swap Nodes in Pairs 交换相邻结点
给定一个链表,对每两个相邻的结点作交换并返回头节点.例如:给定 1->2->3->4,你应该返回 2->1->4->3.你的算法应该只使用额外的常数空间.不要修改列 ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->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-> ...
- [leetcode]24. Swap Nodes in Pairs交换链表的节点
感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...
- [LeetCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
随机推荐
- Matlab中@与函数调用
function m f=@(x) x^2; y(f,3); function y(f,x) disp(num2str(f(x))); end end 函数调用另一个函数的时候,把另一个函数名作为参数 ...
- 剑指offer--面试题10--相关
题目一:判断一个整数是不是2的n次幂 实现大概如下: int main() { ; )) == ) //重要!! std::cout<<"YES!"<<st ...
- What is the difference between database table and database view?
The database table has a physical existence in the database. A view is a virtual table, that is one ...
- C# get set方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 360抢票 网站维护中 && 你的登录被踢了!
本来在超市犹豫到底该买哪种暖手袋,犹豫了差不多半个多小时,还没决定好,一看时间还有8分钟到10点,遂狂奔回寝室抢票. 结果,360抢票被12306秒了—— 猜测原因是12306的验证码改了(变成动态的 ...
- unity3d泰斗破坏神2----课程列表
免费 课时1:泰斗破坏神第一支预告视频 01:32免费 课时2:泰斗破坏神第二支预告视频 01:58第 1 章 : 游戏开始 用户登录 服务器选择课时3:游戏开始 用户登录 服务器选择课时4:素材介绍 ...
- C++:构造函数的重载
构造函数的重载的判断条件是:参数的个数或者类型不同,其他要保持一样 举例说明: 方式一:在类中声明重载构造函数,在类外定义 //第一种方法: #include<iostream> usin ...
- kaili开启sshd服务
使用xshell远程连接kali 2.0时要开启kaili上的sshd服务,具体方法如下: 命令: vim /etc/ssh/sshd_config # Package generated confi ...
- 基于条件随机场(CRF)的命名实体识别
很久前做过一个命名实体识别的模块,现在有时间,记录一下. 一.要识别的对象 人名.地名.机构名 二.主要方法 1.使用CRF模型进行识别(识别对象都是最基础的序列,所以使用了好评率较高的序列识别算法C ...
- Xcode插件开发
一.安装模板 1.git clone https://github.com/kattrali/Xcode-Plugin-Template.git 2.cd Xcode-Plugin-Template ...