Leetcode--Swap Nodes in Pairs
最傻的方法:
ListNode *swapPairs(ListNode *head) {
if (head == NULL)
return NULL;
ListNode *temp = );
ListNode *head_2 = temp;
while (head != NULL && head->next != NULL) {
temp = temp->next = new ListNode(head->next->val);
temp = temp->next = new ListNode(head->val);
head = head->next->next;
}
if (head != NULL)
temp->next = head;
return head_2->next;
}
好一点的方法
ListNode* swapPairs(ListNode* head) {
if(!head || !head->next)
return head;
ListNode * temp = head->next;
head->next = swapPairs(temp->next);
temp->next = head;
return temp;
}
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 exam ...
- [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 成对交换节点
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 ...
- Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [LeetCode]Swap Nodes in Pairs 成对交换
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- Android 监听返回键、HOME键
拦截返回键,HOME键,继承BaseActivity即可 import android.app.Activity; import android.content.BroadcastReceiver; ...
- LINUX+Vmware+SVN的配置和安装
LINUX+Vmware+SVN的配置和安装 验证SVN安装了没有 svnserve --version 查看CentOS自带JDK是否已安装. ◆输入:yum list installed |gre ...
- 设置 phoneGap/Cordova 3.4 应用程序启动动画闪屏 SplashScreen
当Cordova 程序打包并安装到手机中后,我们会发现启动程序时,会有数秒的黑屏现象,常见的解决方法则是设置闪屏后面. 这里以 Android 程序为例,介绍Cordova设置启动画面的方法. 1. ...
- cmd常用命令符
想成为电脑高手必须掌握的八个cmd 命令 http://www.cr173.com/html/3917_1.html ping命令的其他技巧:在一般情况下还可以通过ping对方让对方返回给你的TTL值 ...
- Android 子线程测试
private volatile boolean mStopped = false; private int i; TextView tv1; TextView tv2; @Override prot ...
- Graphics平移缩放旋转(转载)+点睛
点睛:可以进行多次旋转和平移,也就是平移以后再平移,旋转以后再旋转,有时候一次达不到要求,如果你想一次调整完美的话很麻烦,所以最好多次,上代码 private void btnTranslate_Cl ...
- EF简单的添加修改删除基本语法
using ( androidhiveEntities db = new androidhiveEntities() ) { #re ...
- Xamarin.Forms ListView点击按钮刷新最新数据
最近在研究Xamarin的东西,做到ListView遇到了一些瓶颈,像在数据庞大的情况下,该怎么针对ListView中的数据分组呢? 基于能力有限的问题,暂时写了一个只可以实现功能的临时解决方案,毕竟 ...
- Web API
https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api ...
- ifconfig 下面的一些字段(errors, dropped, overruns)
一台机器经常收到丢包的报警,先看看最底层的有没有问题: # ethtool em2 | egrep 'Speed|Duplex' Speed: 1000Mb/s Duplex: Full # etht ...